I have written a simple application in C# it uses an XML file. When the program loads it must take values from the file and load them into an arraylist, The problem is when I move the program into a different PC I must change the file location manually. How can I make the file name not to change even when running the program in a different PC.
-
1What file location are you talking about? Where do you need to change it manually?Daniel Hilgarth– Daniel Hilgarth2012-09-11 13:46:05 +00:00Commented Sep 11, 2012 at 13:46
-
sounds like you just need to pass a filename as parameter to the application -> stackoverflow.com/questions/653563/…codeling– codeling2012-09-11 13:47:50 +00:00Commented Sep 11, 2012 at 13:47
-
Why do you have to change the file name? You need to explain more about how you install your program on other machines, and also where the Xml file is supposed to be relative to your program.Sam Goldberg– Sam Goldberg2012-09-11 14:01:28 +00:00Commented Sep 11, 2012 at 14:01
Add a comment
|
3 Answers
There are a few ways to solve this. One is to pass the file name on as a command line argument. For example:
public static void main(string[] args)
{
// Use the first argument on the command line
string file = args[0];
}
Another is to include it in the app.config file. See https://codereview.stackexchange.com/questions/186/getting-setting-default-values-from-my-app-config for a good example.
Comments
You can store the file inside the assembly location or some common location
string myfile = System.IO.Path.Combine(Assembly.GetExecutingAssembly().Location,"your file name");
1 Comment
S3ddi9
I think this is the best answer