filestream - C# Creating and opening file in isolation throws IOException -


i made small program demonstrates problem. creates file, , tries open @ same address. ioexception sharing violation on path thrown when tries open file. why happen? can see file being created.

using system; using system.io;  namespace filetestproject { class mainclass {     private static string address = "/users/jamessullivan/desktop/testfile.dat";      public static void main ()     {         file.create(address);         filestream file = file.open(address, filemode.open);     } } } 

you can see problem looking documentation.

although example meaningless, here explanation of problem.

create.file(string) returns filestream holds unmanaged resource called file handle can't create filestream which should hold same handle.

thous, should free resource after reusing it.

this simple consept:

using(file.create(address)) {}  using(filestream file = file.open(address, filemode.open) { } 

Comments