c# - Rename File open by self -


my program logging data file, @ same time user interface displays incoming data live. want logged data on disk within second or 2 if computer/program/os/whatever shuts down. data coming in @ least 100 times/sec.

i want user able give log-file new name, while logging active. problem can't change name of file while open, if same process.

test case:

        string filename1 = "test.txt";         string filename2 = "test2.txt";          using (streamwriter sw = new streamwriter(new filestream(filename1, filemode.create)))         {             sw.writeline("before");             file.move(filename1, filename2);  //<<-- ioexception - process cannot access file because being used process.             sw.writeline("after");         } 

so, how rename file process while same process having stream file open?

you should close first stream, rename file, reopen stream:

using (streamwriter sw = new streamwriter(new filestream(filename1, filemode.create))) {   sw.writeline("before");   sw.close(); }  file.move(filename1, filename2);  using (streamwriter sw = new streamwriter(new filestream(filename2, filemode.append))) {   sw.writeline("after"); } 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -