Today, I have organized some common components written before and found that a class for the operation of the file directory is more useful, so it has been modified him. Some methods are to see some netizens in the community. , Including files and subdirectories, moving directory files, and subdirectories, generating XML tree files, etc.
Using system;
Using system.io;
USING SYSTEM.XML;
Namespace zhzuo
{
///
/// FileDirectoryUtility class, method does not contain exception processing
/// summary>
Public Class FileDirectoryUtilityUtility
{
///
/// path splitter
/// summary>
Private const string path_split_char = "//";
///
/// Constructor
/// summary>
Private fileDirectoryutility ()
{
}
///
/// Copy all files of the specified directory, not contain files in subdirectories and subdirectories
/// summary>
/// Original directory param>
/// Target directory param>
/// If true, it means that the same name file is overwritten, otherwise it is not overwritten param>
Public Static Void CopyFiles (String Sourcedir, String Targetdir, Bool Overwrite)
{
CopyFiles (Sourcedir, Targetdir, Overwrite, False);
}
///
/// Copy all files for the specified directory
/// summary>
/// Original directory param>
/// Target directory param>
/// If true, override the same name file, otherwise it is not overwriting param>
/// If true, contains the directory, otherwise it does not include param>
Public Static Void CopyFiles (String Sourcedir, String Targetdir, Bool Overwrite, Bool Copysubdir)
{
// Copy the current directory file
FOREACH (String SourceFileName in Directory.Getfiles (SOURCEDIR)
{
String TargetFileName = path.combine (targetdir, SourceFileName.Substring (SourceFileName.lastIndexof (path_split_char) 1);
File.exists (targetfilename))
{
IF (OverWrite == True)
{
File.setttributes (targetfilename, fileattributes.normal); file.copy (SourceFileName, TargetFileName, Overwrite);
}
}
Else
{
File.copy (SourceFileName, TargetFileName, Overwrite);
}
}
// Copy subdirectory
IF (CopySubdir)
{
FOREACH (String SourceSubdir in Directory.GetDirector (SOURCEDIR)
{
String targetsubdir = path.combine (targetdir, SourceSubdir.substring (SourceSubdir.lastIndexof (Path_Split_CHAR) 1);
IF (! Directory.exists (Targetsubdir))
Directory.createDirectory (Targetsubdir);
CopyFiles (SourceSubdir, Targetsubdir, Overwrite, True);
}
}
}
///
/// Cut all files of the specified directory, do not include subdirectory
/// summary>
/// Original directory param>
/// Target directory param>
/// If true, override the same name file, otherwise it is not overwriting param>
Public Static Void MoveFiles (String Sourcedir, String Targetdir, Bool Overwrite)
{
Movefiles (Sourcedir, Targetdir, Overwrite, False);
}
///
/// Cut all files for the specified directory
/// summary>
/// Original directory param>
/// Target directory param>
/// If true, override the same name file, otherwise it is not overwriting param>
/// If true, contain the directory, do not include param>
Public Static Void MoveFiles (String Sourcedir, String Targetdir, Bool OverWrite, Bool MoveSubdir)
{
/ / Mobile current directory file
FOREACH (String SourceFileName in Directory.Getfiles (SOURCEDIR)
{
String TargetFileName = path.combine (targetdir, SourceFileName.Substring (SourceFileName.lastIndexof (path_split_char) 1);
File.exists (targetfilename))
{
IF (OverWrite == True)
{
File.setttributes (targetfilename, fileattributes.normal); file.delete (TargetFileName);
File.Move (SourceFileName);
}
}
Else
{
File.Move (SourceFileName);
}
}
IF (movesubdir)
{
FOREACH (String SourceSubdir in Directory.GetDirector (SOURCEDIR)
{
String targetsubdir = path.combine (targetdir, sources ");
IF (! Directory.exists (Targetsubdir))
Directory.createDirectory (Targetsubdir);
Movefiles (SourceSubdir, Targetsubdir, Overwrite, True);
Directory.delete (SourceSubdir);
}
}
}
///
// / Delete all the files of the specified directory, do not include subdirectory
/// summary>
/// Operating directory param>
Public static void deletefiles (String Targetdir)
{
Deletefiles (Targetdir, False);
}
///
// / Delete all files and subdirectories of the specified directory
/// summary>
/// Operating directory param>
/// If true, the operation of the subdirectory param>
Public Static void deletefiles (String Targetdir, Bool Delsubdir)
{
FOREACH (String FileName in Directory.Getfiles (Targetdir))
{
File.setttributes (filename, fileAttributes.Normal);
File.delete (filename);
}
IF (DelsubDir)
{
DirectoryInfo Dir = New DirectoryInfo (Targetdir);
Foreach (DirectoryInfo Subdi in Dir.getdirectories ())
{
Deletefiles (Subdi.Fullname, True);
Subdi.delete ();
}
}
}
///
// Create a specified directory
/// summary>
/// param>
Public Static Void CreatedIRectory (String Targetdir)
{
DirectoryInfo Dir = New DirectoryInfo (Targetdir);
IF (! Dir.exists)
Dir.create ();
}
///
/// Create a subdirectory
/// summary>
/// directory path param> /// subdirectory name param>
Public static void createDirectory (String Parentdir, String SubDirname)
{
CreatedIRectory (PARENTDIR PATH_SPLIT_CHAR SUBDIRNAME);
}
///
/// Delete the specified directory
/// summary>
/// directory path param>
Public Static Void deletedIRectory (String Targetdir)
{
DirectoryInfo Dirinfo = New DirectoryInfo (Targetdir);
IF (Dirinfo.exists)
{
Deletefiles (targetdir, true);
Dirinfo.delete (TRUE);
}
}
///
/// Delete all subdirectories of the specified directory, not including deletion of the current directory file
/// summary>
/// directory path param>
Public Static void deleteSubdirectory (String Targetdir)
{
FOREACH (String Subdir in Directory.getdirector))
{
DeletedIRectory (Subdir);
}
}
///
// / Generate XML documentation and files in the specified directory
/// summary>
/// root directory param>
///
Public static xmldocument createXML (String Targetdir)
{
XMLDocument myDocument = new xmldocument ();
Xmldeclarative Declaration = MyDocument.createxmldeclaration ("1.0", "UTF-8", NULL);
MyDocument.Appendchild (Declaration);
Xmlelement RootElement = MyDocument.createElement (targetdir.substring (targetdir.lastIndexof (path_split_char) 1));
MyDocument.Appendchild (RootElement);
FOREACH (String FileName in Directory.Getfiles (Targetdir))
{
Xmlelement CHildElement = MyDocument.createElement ("file");
ChildElement.innertext = filename.substring (filename.lastIndexof (path_split_char) 1);
RootElement.Appendchild (childlement);
FOREACH (String Directory in Directory.GetDirector))
{
Xmlelement CHildElement = MyDocument.createElement ("Directory");
ChildElement.setttribute ("name", Directory.substring (Directory.lastIndexof 1));
RootElement.Appendchild (ChildElement);
Createbranch (Directory, ChildElement, MyDocument);
}
Return myDocument;
}
///
/// Generate an XML branch
/// summary>
/// subdirectory param>
/// Parent directory XMLDocument param>
/// Xmldocument object param>
Private Static Void Createbranch (String Targetdir, XmLelement XMLNode, XmLDocument MyDocument)
{
FOREACH (String FileName in Directory.Getfiles (Targetdir))
{
Xmlelement CHildElement = MyDocument.createElement ("file");
ChildElement.innertext = filename.substring (filename.lastIndexof (path_split_char) 1);
XMLNode.Appendchild (childlement);
}
FOREACH (String Directory in Directory.GetDirector))
{
Xmlelement CHildElement = MyDocument.createElement ("Directory");
ChildElement.setttribute ("name", Directory.substring (Directory.lastIndexof 1));
XMLNode.Appendchild (childlement);
Createbranch (Directory, ChildElement, MyDocument);
}
}
}
}
The entire class is relatively simple, there is no way to find the relevant finding file directory; there is no processing for exceptions, I think if there is an abnormally thrown or better outside this class. If you have any netizens, please contact me, or leave a message on Blog. In addition, when testing this class, I found a bug, I didn't know the bug and .NET Framework1.1 of the Windows system.