private static DataTable ShowSharedFolders(string userName) { var sharedFolders = new DataTable(); sharedFolders.Columns.Add("Name"); sharedFolders.Columns.Add("Type"); sharedFolders.Columns.Add("Size"); sharedFolders.Columns.Add("UploadTime"); sharedFolders.Columns.Add("Location"); sharedFolders.Columns.Add("FullPath"); string[] subFolders = Directory.GetDirectories(userName); foreach (string subFolderPath in subFolders) { var subFolder = new DirectoryInfo(subFolderPath); sharedFolders.Rows.Add(subFolder.Name, "Folder", "", subFolder.CreationTime.ToString(CultureInfo.InvariantCulture), subFolder.Parent.FullName, subFolder.FullName); } string[] files = Directory.GetFiles(userName); foreach (string filePath in files) { var file = new FileInfo(filePath); sharedFolders.Rows.Add(file.Name, file.Name.LastIndexOf('.') < 0 ? "" : file.Name.Substring(file.Name.LastIndexOf('.')).ToUpper(), CommonUse.FormatFileSize((long)file.Length), file.CreationTime.ToString(CultureInfo.InvariantCulture), file.DirectoryName, file.FullName); if (file.Name.Equals("readme.html")) { FilesObject.DownloadFile(filePath); } } return(sharedFolders); }
/// <summary> /// Get all files and subfolders of the folder /// </summary> /// <param name="folderPath">The name of the folder from which you want to get /// files and folders</param> /// <returns>All files and subfolders in the folder</returns> public static DataTable GetAllItemsInTheDirectory(string folderPath) { var dtAllItems = new DataTable(); dtAllItems.Columns.Add("Type"); dtAllItems.Columns.Add("Name"); dtAllItems.Columns.Add("Size"); dtAllItems.Columns.Add("UploadTime"); dtAllItems.Columns.Add("Location"); dtAllItems.Columns.Add("FullPath"); // Get all subfolder in the folder <folderPath> string[] subFolders = Directory.GetDirectories(folderPath); foreach (string subFolderPath in subFolders) { var subFolder = new DirectoryInfo(subFolderPath); dtAllItems.Rows.Add("Folder", subFolder.Name, "", subFolder.CreationTime.ToString(), subFolder.Parent.FullName, subFolder.FullName); } // Get all files in the folder <folderPath> string[] files = Directory.GetFiles(folderPath); foreach (string filePath in files) { var file = new FileInfo(filePath); dtAllItems.Rows.Add( file.Name.LastIndexOf('.') < 0 ? "" : file.Name.Substring(file.Name.LastIndexOf('.')).ToUpper(), file.Name, CommonUse.FormatFileSize((long)file.Length), file.CreationTime.ToString(), file.DirectoryName, file.FullName); } return(dtAllItems); }
protected void btnUpload_Click(object sender, EventArgs e) { // The file Extension string fileExtension = string.Empty; // The chosen file name string fileName = string.Empty; // The new file name in the server string newFileName = string.Empty; // Check if choosed a file if (!fuChooseFile.HasFile) { lbMessage.Text = "Please choose the file you want to upload. " + "Note: The file size cannot be zero."; return; } fileExtension = Path.GetExtension(fuChooseFile.FileName); fileName = string.Format("{0}\\{1}", CurrentLocation, (string.IsNullOrEmpty(fileExtension) ? fuChooseFile.FileName : fuChooseFile.FileName.Replace(fileExtension, ""))); if (fileExtension.ToLower() == ".exe" || fileExtension.ToLower() == ".msi") { lbMessage.Text = "The file you want to upload cannot be a .exe or .msi file."; return; } newFileName = fileName; // Check file size if (fuChooseFile.PostedFile.ContentLength >= 1024 * 1024 * 40) { lbMessage.Text = "The file you want to upload cannot be larger than 40 MB."; return; } try { // If there is already a file with the same name in the system,rename // and then upload the file . int i = 0; while (File.Exists(newFileName + fileExtension)) { i++; newFileName = string.Format(fileName + "({0})", i); } fuChooseFile.SaveAs(Utilities.FixRoot(newFileName + fileExtension)); lbMessage.Text = string.Format("The file \"{0}\" was uploaded successfully!", Path.GetFileName(fileName)); ShowFolderItems(); } catch (HttpException he) { lbMessage.Text = string.Format("File {0} upload failed because of the following error:{1}.", fuChooseFile.PostedFile.FileName, he.Message); } var _file2 = new FileInfo(newFileName); var db = new CloudAppDbEntities(); var _file = new UserFile(); //_file.FName = Path.GetFileName(newFileName); _file.FName = newFileName; _file.FileName = Path.GetFileName(fileName); _file.UploaderID = db.User.Find(User.Identity.Name).UserID; _file.FileSize = CommonUse.FormatFileSize((long)fuChooseFile.PostedFile.ContentLength); //_file.FileSize = CommonUse.FormatFileSize(_file2.Length); //_file.FileType = Path.GetExtension(fileName); //_file.FileType = _file2.Name.LastIndexOf('.') < 0 ? "" : _file2.Name.Substring(_file2.Name.LastIndexOf('.')).ToUpper(); _file.FileType = fileExtension; _file.UploadTime = _file2.CreationTime; _file.CourseID = null; _file.ClassID = null; db.UserFile.Add(_file); db.SaveChanges(); }