tags:

views:

44

answers:

3

I have the following code in an Winform Application

 String[] lines = { "LAST MESSAGE", "101" };
 File.WriteAllLines("MPP_Config.txt", lines);

On my Development system, the file gets created under Bin\Debug...

Where will the file be created in the client once it is installed in the client system ?

I deploy to a website using Click once Deployment...

+1  A: 

In the current directory. This means that it might be hard to predict with 100% certainty. If you want to control it, you can use Environment.CurrentDirectory but (as Sam points out in the comments) this may not be a good idea since other code may also depend on the current directory for other purposes.

For instance, the following program will create two different files called "somefile.txt" (given that the exe is not run from the c:\temp directory):

static void Main(string[] args)
{
    File.WriteAllText("somefile.txt", "some text");
    Environment.CurrentDirectory = @"c:\temp";
    File.WriteAllText("somefile.txt", "some text");
}
Fredrik Mörk
isn't it a bad idea to set the `Environment.CurrentDirectory` as this could have effects on other things which use it?
Sam Holder
@Sam: I didn't say it was a good idea (in fact I contemplated including a word of warning in the answer). I completely agree with you. (answer updated)
Fredrik Mörk
@Sam Every process gets their own copy of the Environment variables. If you change the Environment.CurrentDirectory, it wont impact the other processes.
NimsDotNet
@NimsDotNet, indeed but I still think that changing it in some place, even just for the current process, has the potential to lead to a hard to find bug. This might be ok at app initialization time, but is unlikely to be a good choice at any other time
Sam Holder
+3  A: 

I believe it will be created in current working directory of the application. The application might not have access to this directory, especially on systems with UAC, Vista and windows 7. You should probably think about using the application data directory instead.

String[] lines = { "LAST MESSAGE", "101" };
String fileName = Path.combine(System.Deployment.Application.ApplicationDeployment.CurrentDeployment.DataDirectory,"MPP_Config.txt");
File.WriteAllLines(fileName, lines);
Sam Holder
Thanks for the solution...
The King
A Small Change ::::: Path.Combine(System.Deployment.Application.ApplicationDeployment.CurrentDeployment.DataDirectory, "MPP_Config.txt");
The King
edited answer with that change. thanks.
Sam Holder
+2  A: 

I'm guessing it gets created in the debug folder becuse you have been running it in debug mode. If you ran it in release mode then it will be saved in the bin/release folder.

In other words it will be created in the directory in which the application resides. Why not try it? I.e. copy your exe across...

James Wiseman
it should be pointed out that the directory in which the application resides may not be the current working directory, it just happens to be by default when you are running from VS I think
Sam Holder