zrubi zrubi - 3 months ago 22
C# Question

Directory path for executable

Hello I’m trying create directory file with text document inside my first windows form application executable, but something is wrong here:

I want make it available with exe file for other local user computers:

string dir = @"C:\Users\Public\AppData\Roaming\AppFolder\document.txt";
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(Path.GetDirectoryName(dir));
var stream = File.CreateText(dir);
}


but I got this:


An unhandled exception of type 'System.IO.IOException' occurred in
mscorlib.dll

Additional information: The process cannot access the file
'C:\Users\Public\AppData\Roaming\AppFolder\doc.txt'
because it is being used by another process.

Answer

I guess you need to issue the commands for the stream to be accessible anywhere else

stream.Flush();
stream.Close();