C # operations on files and folders (copy, delete, add, and set read-only)-Technology & amp; Sharing

Source: Internet
Author: User

C # operations on files and folders (copy, delete, add, and set read-only)-Technology & sharing 
Using System; using System. IO; using System. Windows; using System. Windows. Events; using System. Xml; namespace WorkItemCreateBussiness. HzClass {public class HzFile {////// Delete all files and subfolders in the specified folder //////Path of the deleted folder///Whether to delete the specified folder; optional values: true: Delete; false: Do not delete///
 
  
Returns true if the call is successful, and false if the call fails.
 Public bool DeleteFolder (string fileFolder, bool isDeleteCurrentFolder) {try {if (Directory. exists (fileFolder) {foreach (var file in Directory. getFiles (fileFolder ,"*. * ", SearchOption. allDirectories) {File. setAttributes (file, System. IO. fileAttributes. normal); File. delete (file);} Directory. delete (fileFolder, true); if (! IsDeleteCurrentFolder) {Directory. CreateDirectory (fileFolder) ;}} catch (Exception ex) {MessageBox. Show (ex. Message); return false;} return true ;}////// Hzfile /// get the extension of the specified file //////Absolute path of the specified file folder///
 
  
Returns the file extension. If the file extension is null or the file path is incorrect, null is returned.
 Public string GetFileExtension (string filePath) {string filePathExtension = ""; try {if (Directory. exists (filePath) {filePathExtension = filePath. substring (filePath. lastIndexOf (". ") + 1) ;}} catch (Exception ex) {MessageBox. show (ex. message); return "";} return filePathExtension;} public void CopyBaseSql (string fromFolder, string toFolder) // copy the standard SQL file in the SQL directory to the \ Upgrade \ BaseCode \ Code directory {try {string shortFolderName; if (toFolder. indexOf ("related documentation")>-1) {if (Directory. exists (toFolder) {Directory. delete (toFolder, true);} Directory. createDirectory (toFolder);} // File. setAttributes (to, FileAttributes. hidden); // subfolder foreach (string sub in Directory. getDirectories (fromFolder) {shortFolderName = sub. substring (sub. lastIndexOf ("\") + 1, sub. length-sub. lastIndexOf ("\")-1); CopyBaseSql (sub + "\", toFolder + "\" + Path. getFileName (sub);} // file foreach (string file in Directory. getFiles (fromFolder) {File. copy (file, toFolder + "\" + Path. getFileName (file), true) ;}} catch {// MessageBox. show ("failed to create SQL file ");}}////// Hzfile /// copy all subdirectories and files in the fromFolder folder to the toFolder folder //////Folder to be copied///Path of the copied folder///
 
  
If an exception exists, true is returned; otherwise, false is returned.
 Public bool CopyFolder (string fromFolder, string toFolder) {try {if (Directory. exists (toFolder) {Directory. delete (toFolder, true);} Directory. createDirectory (toFolder); // subfolder foreach (string sub in Directory. getDirectories (fromFolder) {// shortFolderName = sub. substring (sub. lastIndexOf ("\") + 1, sub. length-sub. lastIndexOf ("\")-1); CopyFolder (sub + "\", toFolder + "\" + Path. getFileName (sub);} // file foreach (string file in Directory. getFiles (fromFolder) {File. copy (file, toFolder + "\" + Path. getFileName (file), true) ;}} catch (Exception ex) {// MessageBox. show ("failed to create SQL file"); return false;} return true ;}////// Hzfile /// copy the fromFolder folder to the toFolder folder //////Folder to be copied///Path of the copied folder///
 
  
If an exception exists, true is returned; otherwise, false is returned.
 Public bool CopyFileToCommonFolder (string fromFolder, string toCommonFolder) {try {if (Directory. exists (toCommonFolder) {Directory. delete (toCommonFolder, true);} Directory. createDirectory (toCommonFolder); // file foreach (string file in Directory. getFiles (fromFolder ,"*. * ", SearchOption. allDirectories) {File. copy (file, toCommonFolder + "\" + Path. getFileName (file), true) ;}} catch (Exception ex) {// MessageBox. show ("failed to create SQL file"); return false;} return true ;}////// Hzfile /// copy the fromFolder folder to the toFolder folder ,//////Folder to be copied///Path of the copied folder///
 
  
If an exception exists, true is returned; otherwise, false is returned.
 Public bool CopyFileToCommonFolder (string fromFolder, string toCommonFolder, XmlDocument xmlDoc, string xPath) {try {if (Directory. exists (toCommonFolder) {Directory. delete (toCommonFolder, true);} Directory. createDirectory (toCommonFolder); // file foreach (string file in Directory. getFiles (fromFolder ,"*. * ", SearchOption. allDirectories) {string fileFullName = Path. getFullPath (file); string fileName = Path. getFileName (file); string folder = fileFullName. substring (fromFolder. length, fileFullName. length-fromFolder. length-fileName. length-1); XmlNodeList fileNode = xmlDoc. selectNodes (@ "Mysoft. data/UpgradeData/Package/Code/File [@ fileName = "+" '"+ fileName +"' and @ folder = "+" '"+ folder +"' "+"] "); if (fileNode. count = 1) {string updateFileName = fileNode. item (0 ). selectSingleNode ("FileContent "). attributes ["fileName"]. value; File. copy (file, toCommonFolder + "\" + updateFileName, true);} // string fileName = FileNode. childNodes [0]. attributes ["fileName"]. value; // File. copy (file, toCommonFolder + "\" + fileName, true) ;}} catch (Exception ex) {// MessageBox. show ("failed to create SQL file"); return false;} return true ;}////// Hzfile /// determine whether a file of the rightFolder type exists in leftFolder //////Pre-fix folder///Repaired folder///You need to confirm whether the existing file is rightFile.///
 
  
RightFile in the map path of the folder on the right, which also exists in the map directory of the folder on the left. If yes, edit is returned; otherwise, add is returned.
 Public string DiffFileExit (string leftFolder, string rightFolder, string rightFile) {try {if (Directory. exists (leftFolder) & Directory. exists (rightFolder) {// if (File. exists (leftFolder. substring (0, rightFile. indexOf ("after") + 6) + rightFile. substring (rightFile. indexOf ("after") + 5) if (File. exists (leftFolder + rightFile) {return "edit" ;}else {return "add" ;}} else {return ";}} catch (Exception ex) {// MessageBox. show (ex. message); return "";} return "";}////// Read-Only folders, subfolders, and files //////Specify the path to the read-only folder///
 
  
Returns true if the call is successful, and false if the call fails.
 Public bool SetNotReadOnly (string dirPath) {try {string [] dirPathes = Directory. getDirectories (dirPath ,"*. * ", SearchOption. allDirectories); foreach (var dp in dirPathes) {if (! Directory. exists (dp) {continue;} DirectoryInfo dir = new DirectoryInfo (dp); dir. attributes = FileAttributes. normal & FileAttributes. directory; string [] filePathes = Directory. getFiles (dp ,"*. * ", SearchOption. allDirectories); foreach (var fp in filePathes) {File. setAttributes (fp, FileAttributes. normal) ;}} catch (Exception ex) {MessageBox. show (ex. message); return false ;}return true ;}}}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.