/// <summary> /// Deletes a directory specified in the MDirectoryProfile in the current directory. /// </summary> /// <param name="currentDirectoryName">string</param> /// <param name="directoryProfile">MDirectoryProfile</param> /// <returns></returns> public static string DeleteDirectory(string currentDirectoryName, MDirectoryProfile directoryProfile) { if (directoryProfile == null) { throw new ArgumentNullException("directoryProfile", "directoryProfile can not be null reference (Nothing in Visual Basic)"); } string mRetVal = null; WindowsImpersonationContext mImpersonatedUser = null; mRetVal = "Successfully deleted the directory(s)"; try { if (directoryProfile.Impersonate) { mImpersonatedUser = WebImpersonate.ImpersonateNow(directoryProfile.ImpersonateAccount, directoryProfile.ImpersonatePassword); } Directory.Delete(currentDirectoryName); } catch (IOException ex) { mRetVal = ex.Message.ToString(); } finally { if (directoryProfile.Impersonate) { // Stop impersonating the user. if ((mImpersonatedUser != null)) { mImpersonatedUser.Undo(); } } } return(mRetVal); }
/// <summary> /// Up loads file from an HtmlInputFile to the directory specified in the MDirectoryProfile object with a specific file name. /// </summary> /// <param name="fileName">string</param> /// <param name="uploadFile">HtmlInputFile</param> /// <param name="currentDirectory">string</param> /// <param name="directoryProfile">MDirectoryProfile</param> /// <returns>string</returns> public static string DoUpload(string fileName, HttpPostedFile uploadFile, string currentDirectory, MDirectoryProfile directoryProfile) { if (directoryProfile == null) { throw new ArgumentNullException("directoryProfile", "directoryProfile can not be null reference (Nothing in Visual Basic)"); } string mRetVal = "Upload successfull"; char mDirectorySeparatorChar = System.IO.Path.DirectorySeparatorChar; WindowsImpersonationContext mImpersonatedUser = null; if ((uploadFile != null)) { try { if (directoryProfile.Impersonate) { mImpersonatedUser = WebImpersonate.ImpersonateNow(directoryProfile.ImpersonateAccount, directoryProfile.ImpersonatePassword); } string mFilename = uploadFile.FileName; if (fileName != null) { mFilename = fileName; } System.IO.Path.GetFileName(uploadFile.FileName); uploadFile.SaveAs(currentDirectory + mDirectorySeparatorChar.ToString() + mFilename); } catch (IOException ex) { Logger mLog = Logger.Instance(); mLog.Error(ex); mRetVal = "Failed uploading file"; } finally { if (directoryProfile.Impersonate) { // Stop impersonating the user. if ((mImpersonatedUser != null)) { mImpersonatedUser.Undo(); } } } } else { mRetVal = "fileToUpload cannot be a null reference (Nothing in Visual Basic)! or Nothing."; } return(mRetVal); }
/// <summary> /// Renames a directory from the "source" to the "destination" /// </summary> /// <param name="sourceDirectoryName">string</param> /// <param name="destinationDirectoryName">string</param> /// <param name="directoryProfile">MDirectoryProfile</param> /// <returns>string</returns> /// <remarks>The MDirectoryProfile object is used for impersonation if necessary.</remarks> public static string RenameDirectory(string sourceDirectoryName, string destinationDirectoryName, MDirectoryProfile directoryProfile) { string mRetVal = null; WindowsImpersonationContext mImpersonatedUser = null; mRetVal = "Successfully renamed the directory!"; if (directoryProfile != null) { try { if (directoryProfile.Impersonate) { mImpersonatedUser = WebImpersonate.ImpersonateNow(directoryProfile.ImpersonateAccount, directoryProfile.ImpersonatePassword); } Directory.Move(sourceDirectoryName, destinationDirectoryName); } catch (IOException ex) { Logger mLog = Logger.Instance(); mLog.Error(ex); mRetVal = ex.Message.ToString(); } finally { if (directoryProfile.Impersonate) { // Stop impersonating the user. if ((mImpersonatedUser != null)) { mImpersonatedUser.Undo(); } } } } else { throw new ArgumentNullException("directoryProfile", "directoryProfile can not be null reference (Nothing in Visual Basic)"); } return(mRetVal); }
/// <summary> /// Creates the directory specified in the MDirectoryProfile given the currentDirecty. /// </summary> /// <param name="currentDirectory">string</param> /// <param name="newDirectoryName">string</param> /// <param name="directoryProfile">MDirectoryProfile</param> /// <returns>string</returns> public static string CreateDirectory(string currentDirectory, string newDirectoryName, MDirectoryProfile directoryProfile) { if (directoryProfile == null) { throw new ArgumentNullException("directoryProfile", "directoryProfile can not be null reference (Nothing in Visual Basic)"); } string mRetVal = null; mRetVal = "Successfully created the new directory!"; WindowsImpersonationContext impersonatedUser = null; try { if (directoryProfile.Impersonate) { impersonatedUser = WebImpersonate.ImpersonateNow(directoryProfile.ImpersonateAccount, directoryProfile.ImpersonatePassword); } Directory.CreateDirectory(currentDirectory + "\\" + newDirectoryName); } catch (IOException ex) { Logger mLog = Logger.Instance(); mLog.Error(ex); mRetVal = "Directory was not created!"; } finally { if (directoryProfile.Impersonate) { // Stop impersonating the user. if ((impersonatedUser != null)) { impersonatedUser.Undo(); } } } return(mRetVal); }
/// <summary> /// Returns a table of files and/or directories. /// </summary> /// <param name="path">string</param> /// <param name="directoryProfile">MDirectoryProfile</param> /// <param name="filesOnly">bool</param> /// <param name="columnName">name of the column to sort on</param> /// <param name="sortOrder">the sort direction "ASC" or "DESC"</param> /// <returns>DataTable</returns> public static DataTable GetDirectoryTableData(string path, MDirectoryProfile directoryProfile, bool filesOnly, string columnName, string sortOrder) { if (directoryProfile == null) { throw new ArgumentNullException("directoryProfile", "directoryProfile can not be null reference (Nothing in Visual Basic)"); } DataTable mRetTable = getDataTable(); DataRow mRow = mRetTable.NewRow(); StringBuilder mStringBuilder = new StringBuilder(4096); string[] mDirs = null; char mDirectorySeparatorChar = System.IO.Path.DirectorySeparatorChar; WindowsImpersonationContext mImpersonatedUser = null; if (directoryProfile.Impersonate) { mImpersonatedUser = WebImpersonate.ImpersonateNow(directoryProfile.ImpersonateAccount, directoryProfile.ImpersonatePassword); } mRow["Name"] = mStringBuilder.ToString(); mStringBuilder = new StringBuilder(); // Clear the string builder if (!filesOnly) { try { mDirs = Directory.GetDirectories(path); foreach (string mDirectory in mDirs) { string mDirName = System.IO.Path.GetFileName(mDirectory); mStringBuilder = new StringBuilder(); // Clear the string builder mRow = mRetTable.NewRow(); // Create a new row // Populate the string for the new row mStringBuilder.Append(mDirName); // Populate the cell in the row mRow["Name"] = mStringBuilder.ToString(); mStringBuilder = new StringBuilder(); // Clear the string builder mRow["ShortFileName"] = mStringBuilder.ToString(); mStringBuilder = new StringBuilder(); // Clear the string builder mRow["Extension"] = mStringBuilder.ToString(); mStringBuilder = new StringBuilder(); // Clear the string builder // Populate the cell in the row mRow["Delete"] = mStringBuilder.ToString(); mStringBuilder = new StringBuilder(); // Clear the string builder mStringBuilder.Append("Folder"); // Populate the cell in the row mRow["Type"] = mStringBuilder.ToString(); mStringBuilder = new StringBuilder(); // Clear the string builder mStringBuilder.Append("N/A"); // Populate the cell in the row mRow["Size"] = mStringBuilder.ToString(); mStringBuilder = new StringBuilder(); // Clear the string builder mStringBuilder.Append(Directory.GetLastWriteTime(path + mDirectorySeparatorChar.ToString(CultureInfo.InvariantCulture) + mDirName).ToString()); // Populate the cell in the row mRow["Modified"] = mStringBuilder.ToString(); mStringBuilder = new StringBuilder(); mStringBuilder.Append(mDirectorySeparatorChar.ToString(CultureInfo.InvariantCulture) + mDirName + "\\"); mRow["FullName"] = mStringBuilder.ToString(); mRetTable.Rows.Add(mRow); // Add the row to the table } } catch (IOException ex) { if (mRetTable != null) { mRetTable.Dispose(); } Logger mLog = Logger.Instance(); mLog.Error(ex); throw; } } // Add all of the directories to the table try { DirectoryInfo dirInfo = new DirectoryInfo(path); FileInfo[] files = null; files = dirInfo.GetFiles(); //FileInfo mFileInfo = null; if (mRetTable == null) { } foreach (FileInfo mFileInfo in files) { mRow = mRetTable.NewRow(); string mFilename = mFileInfo.Name; string mShortFileName = mFileInfo.Name; mShortFileName = mFilename.Remove(mFilename.Length - mFileInfo.Extension.Length, mFileInfo.Extension.Length); mStringBuilder = new StringBuilder(); mStringBuilder.Append(mFilename); mRow["Name"] = mFilename.ToString(); mStringBuilder = new StringBuilder(); mStringBuilder.Append("File"); mRow["Type"] = mStringBuilder.ToString(); mStringBuilder = new StringBuilder(); mStringBuilder.Append(mShortFileName); mRow["shortFileName"] = mStringBuilder.ToString(); mStringBuilder = new StringBuilder(); string fileExtension = mFileInfo.Extension; mStringBuilder.Append(fileExtension); mRow["Extension"] = mStringBuilder.ToString(); mStringBuilder = new StringBuilder(); mStringBuilder.Append(mFileInfo.Length.ToFileSize()); mRow["Size"] = mStringBuilder.ToString(); mStringBuilder = new StringBuilder(); mStringBuilder.Append(File.GetLastWriteTime(path + mDirectorySeparatorChar.ToString() + mFileInfo.Name).ToString()); mRow["Modified"] = mStringBuilder.ToString(); mStringBuilder = new StringBuilder(); mStringBuilder.Append(mFileInfo.FullName); mRow["FullName"] = mStringBuilder.ToString(); mRetTable.Rows.Add(mRow); } } catch (IOException ex) { if (mRetTable != null) { mRetTable.Dispose(); } Logger mLog = Logger.Instance(); mLog.Error(ex); throw; } finally { if (directoryProfile.Impersonate) { // Stop impersonating the user. if ((mImpersonatedUser != null)) { mImpersonatedUser.Undo(); } } } // Return the table object as the data source SortTable mSorter = new Framework.Common.SortTable(); String mColName = columnName; mSorter.Sort(mRetTable, mColName, sortOrder); return(mRetTable); }