Write a class of file directory commonly used

xiaoxiao2021-03-05  20

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

///

Public Class FileDirectoryUtilityUtility

{

///

/// path splitter

///

Private const string path_split_char = "//";

///

/// Constructor

///

Private fileDirectoryutility ()

{

}

///

/// Copy all files of the specified directory, not contain files in subdirectories and subdirectories

///

/// Original directory

/// Target directory

/// If true, it means that the same name file is overwritten, otherwise it is not overwritten

Public Static Void CopyFiles (String Sourcedir, String Targetdir, Bool Overwrite)

{

CopyFiles (Sourcedir, Targetdir, Overwrite, False);

}

///

/// Copy all files for the specified directory

///

/// Original directory

/// Target directory

/// If true, override the same name file, otherwise it is not overwriting

/// If true, contains the directory, otherwise it does not include

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

///

/// Original directory

/// Target directory

/// If true, override the same name file, otherwise it is not overwriting

Public Static Void MoveFiles (String Sourcedir, String Targetdir, Bool Overwrite)

{

Movefiles (Sourcedir, Targetdir, Overwrite, False);

}

///

/// Cut all files for the specified directory

///

/// Original directory

/// Target directory

/// If true, override the same name file, otherwise it is not overwriting

/// If true, contain the directory, do not include

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

///

/// Operating directory

Public static void deletefiles (String Targetdir)

{

Deletefiles (Targetdir, False);

}

///

// / Delete all files and subdirectories of the specified directory

///

/// Operating directory

/// If true, the operation of the subdirectory

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

///

///

Public Static Void CreatedIRectory (String Targetdir)

{

DirectoryInfo Dir = New DirectoryInfo (Targetdir);

IF (! Dir.exists)

Dir.create ();

}

///

/// Create a subdirectory

///

/// directory path /// subdirectory name

Public static void createDirectory (String Parentdir, String SubDirname)

{

CreatedIRectory (PARENTDIR PATH_SPLIT_CHAR SUBDIRNAME);

}

///

/// Delete the specified directory

///

/// directory path

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

///

/// directory path

Public Static void deleteSubdirectory (String Targetdir)

{

FOREACH (String Subdir in Directory.getdirector))

{

DeletedIRectory (Subdir);

}

}

///

// / Generate XML documentation and files in the specified directory

///

/// root directory

/// Return to XMLDocument object

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

///

/// subdirectory

/// Parent directory XMLDocument

/// Xmldocument object

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.

转载请注明原文地址:https://www.9cbs.com/read-32255.html

New Post(0)