-2

I am working with c# in window application, and I am hardcoding the file path into the program. But if I move the application to another PC, that absolutely can't find out the directory of the file.

like this:

dataGridView2.DataSource = ReadCsv("C:\\Users\\PC\\Documents\\Visual Studio 2015\\Projects\\print_report_system\\price.csv");

Is it someway to solve this problem?

Sorry for my poor english.

Thx a lot.

Capslock10
  • 796
  • 2
  • 16
  • 37

1 Answers1

0

Yes, put the filepath into the config file app.config

<appSettings>
<add name="filePath" value="PATH HERE"/>
</appSettings>

and use it like this

dataGridView2.DataSource = ReadCsv(ConfigurationManager.AppSettings["filePath"]);

And the change that config file whenever you move the app to the correct file path in the new PC

MoustafaS
  • 1,991
  • 11
  • 20
  • 1
    I would also suggest that they do a check to see if the File Path exist and no matter what at least in code once it reads from the config file, the code can check if(!Directoy.Exist){ } then create it.. better to check than assume, in case the OP forgets to create the Directory. – MethodMan Jul 04 '16 at 03:41
  • Thank for your answer. I have fixed the problem. – Capslock10 Jul 04 '16 at 03:50