Search code examples
c#sharpdevelop

Application throwing my local path as an error on client's machine


I am new to C#. Created a C# application which reads from a file and does some processing. I have manually put the files under myProject/bin/Debug/files/ (Note: manually copy-pasted file, not sure if i need to import in any way through the application - using SharpDevelop IDE). My code reads the file through

string query = File.ReadAllText(@"files\myfile.txt");

After I built and passed it to a client and it's working fine when they click on myproject.exe, it can locate the file. But when they trigger it using windows task scheduler, the error they get is:

System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Windows\system\files\myfile.txt'... at extractor.Program.query() in d:\workspace\test\Program.cs:line 81 (this is my local path, shoud show client's folder path instead)

Need help with:

  1. How should i make the exe pick client's path, not my local?
  2. Is that the right way to add files. Note that I kept it that way because I want to give client the flexibility to change myfile.txt as necessary, without touching the .exe (without the need to build again)
  3. Is this the right way to locate a file (@"files\myfile.txt").

Appreciate help.


Solution

  • use like this

    string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
    String query = File.ReadAllText(Path.Combine(path, "files\\Test.txt"));