Summary of the files and streams

In everyday items often used in manipulating files and streams, the following records with some of my operation and continued to add.

Stream operations can only file instead of a folder

Information file information can be used in conjunction with List <file type> to save the file

1. Delete all files in the directory

foreach (string f in Directory.GetFileSystemEntries(Path))
            {

                if (File.Exists(f))
                {
                    // If there are subfolders delete files 
                    File.Delete (f);

                }
            } // delete the local file

2. Create a folder in the current directory

string path = System.IO.Directory.GetCurrentDirectory();  
string newPath = System.IO.Path.Combine(path, "mySubDirOne");

3. Get all the folder path and in each folder below to find

 DirectoryInfo root = new DirectoryInfo(path);
            foreach (DirectoryInfo d in root.GetDirectories())
   {
          foreach (var item in d.EnumerateFiles()) 
           {

           } 

}

4. File operations necessary to prevent the flow of the stream is closed at the end of the process of occupation

// localpath is a complete file name address
 // OpenWrite is open or create a file is File operations
 // the FileStream stream files become 
the FileStream FS = File.OpenWrite (localpath);
fs.Close();
// path is the full path name of the file
 // OpenRead read 
the FileStream FS = File.OpenRead (path);
fs.Close();

File There are many operating CPOY, DELETE, Exists, MOVE, and so on.

Guess you like

Origin www.cnblogs.com/cdjbolg/p/11835776.html