0

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.

befree2j
  • 361
  • 5
  • 11
  • 1
    What file location are you talking about? Where do you need to change it manually? – Daniel Hilgarth Sep 11 '12 at 13:46
  • sounds like you just need to pass a filename as parameter to the application -> http://stackoverflow.com/questions/653563/passing-command-line-arguments-in-c-sharp – codeling Sep 11 '12 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 Sep 11 '12 at 14:01

3 Answers3

2

Copy theXML file in the same folder or subdfolder of where the program is so it can allways be found using for example:

Path.GetDirectoryName(Application.ExecutablePath);
CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
1

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.

Community
  • 1
  • 1
akton
  • 14,148
  • 3
  • 43
  • 47
1

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");
S3ddi9
  • 2,121
  • 2
  • 20
  • 34
Roshana
  • 428
  • 3
  • 15