File & Directory - reading, writing, etc.

1、Demo1

https://blog.csdn.net/baidu_39486224/article/details/79980758

// ================================================ === file creation, move, delete, generate, compressed 
        Private  void button5_Click ( Object SENDER, EventArgs E) 
        { 
            String VerPath = "" ;
             String FromVerPath = "" ;
             String SFPath = "" ;
             String STEMP = "" , = toPath "" ; 
            NewFileCrePath = Path.Combine (LPATH, SelectDir);
             the try 
            { 
                IF (! Directory.Exists(NewFileCrePath))
                { 
                    Directory.CreateDirectory (NewFileCrePath); // create a new folder
                                                        // MessageBox.Show (sf.ToString ()); 
                    VerPath = NewFileCrePath + @ " \ Version.txt " ; 
                    the FileStream FCREATE = File.Create (VerPath); // create a version of the log file 
                    fcreate.Close ();
                     for ( int SFS = 0 ; SFS <CountFile; SFS ++) // SF is the number, starting from 1, sfs a cycle count, from 0 
                    { 
                        STEMP =SFiles [SFS]; 
                        STEMP = STemp.Substring ( 0 , STemp.Length - . 4 ); // remove the file name suffix 
                        SFPath = LPATH STEMP + + @ " \ " + STEMP; // be moved folder    
                        ToPath = NewFileCrePath + @ " \ " + STEMP; // destination folder 
                        IF (Directory.Exists (SFPath)) 
                        { 
                            Directory.Move (SFPath, toPath); // toPath the name of the file you want to move to where the folder (or rename 
                            + = LPATH SFPath STEMP;
                            Directory.Delete (SFPath, to true ); 
                        } 
                        the else 
                        { 
                            MessageBox.Show ( " want to move the file does not exist! " );
                             Return ; 
                        } 
                        // ================== =========== version generation 
                        FromVerPath = NewFileCrePath + @ " \ " + STEMP + @ " \ Version.txt " ; 
 
                        the FileStream fsver = new new the FileStream (FromVerPath, FileMode.Open); //Right version of every file is extracted 
                        StreamReader Reader = new new StreamReader (fsver, UnicodeEncoding.GetEncoding ( " GB2312 " )); // Chinese, read 
 
                        String [] str = new new String [ 5 ];
                         for ( int i = 0 ; i < 2 ; I ++ ) 
                        { 
                            STR [I] = reader.readLine (); // assignment                      
                        } 
                        the StreamWriter SW = new newThe StreamWriter (VerPath, to true , UnicodeEncoding.GetEncoding ( " GB2312 " )); // write 
                        STR [ 0 ] = STEMP + "                    " + " \ T " + STR [ 0 ]; 
                        sw.WriteLine (STR [ 0 ]); // write back 
                        sw.Close (); 
                        fsver.Close (); 
 
                    } // for end of cycle 
                    CountFile = 0 ;
                     String newzip + = NewFileCrePath" .Zip " ; 
                    ZipDirectory (NewFileCrePath, newzip); // compress
                     // FileUpload (newzip, ""); upload it 
                }
                 the else 
                { 
                    MessageBox.Show ( " file folder to create already exists! " ); 
                } 
            } 
            The catch (Exception EX) 
            { 
                MessageBox.Show ( " error! " );
                 the throw EX; 
            } 
 
        }
View Code

1, the role of FileStream.close (): The file stream is closed.

https://zhidao.baidu.com/question/323934325.html
If you open the file stream reading a file did not close, then the file has been occupied by your program. Others will not be able to operate this file. ( Of course, you can still read, but not write to the file )
, for example, you have two programs, while reading a document, and does not close, then no matter which program can not modify or delete the file.
I read your questioning, you can close the program does not take up memory. Yes, close the program to put the memory release, the problem is that this program is that you do short life? Your development program is for others to come out with it, then this program would have been turned on, you can not just go and close it. For example, a website, it is always on on the Internet. A game that has been open to the players to play. You can always go to this program close? Whether the site or the game, many users will have access to your program, if you do not do free up memory, 100 users will occupy 100 file streams, 1,000 users occupy 1000, your application server and can not hold it and more information. To do so promptly release the memory to ensure the program to run properly.

Although the point, but it does not prevent write txt, eg if you open the txt, but still be able to write;

FileStream fCreate=File.Create(path1);

//fCreate.Close();// here is not closed, it is possible to write back

FileStream fs1 = new FileStream(path1, FileMode.Append);
StreamWriter sw1 = new StreamWriter(fs1);
sw1.WriteLine("1234");
sw1.Flush();
sw1.Close();
fs1.Close();
Console.WriteLine("1234");

 

Guess you like

Origin www.cnblogs.com/wllwqdeai/p/11070392.html