Referencing text file in WPF application

Hi,

I am building a C# WPF application in VS2008 where I need to use a text file. In my development machine it is placed in the root of my application. From the application I can reference it using:

string filename = @"..\..\currencies.txt";

and everything works fine.
Now, When I deploy the application the path produced by the above line translates to "C:\Program files\currencies.txt". How can I make this file reference so it works correct both in my development machine and deployed to a client machine? In the deployment project the file (currencies.txt) is placed in the Application folder and the correct path should be "C:\Program Files\RT solutions\ReceiptScan\currencies.txt".

Best regards
RTSol
private void launchNotepad()
        {
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = "Notepad.exe";
            currURI = @"..\..\currencies.txt";
            startInfo.Arguments = currURI;
            Process process = Process.Start(startInfo);
            process.EnableRaisingEvents = true;
        }

Open in new window

RTSolAsked:
Who is Participating?
 
Meir RivkinFull stack Software EngineerCommented:
hence, your code should look like this:

private void launchNotepad()
        {
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = "Notepad.exe";
            startInfo.Arguments = Path.Combine(Directory.GetCurrentDirectory(),"currencies.txt");
            Process process = Process.Start(startInfo);
            process.EnableRaisingEvents = true;
        }

Open in new window

0
 
Meir RivkinFull stack Software EngineerCommented:
From your application when u wish to reference the txt file u can use Directory.GetCurrentDirectory().
as long as the txt file is in the same folder as the exe.
0
 
Meir RivkinFull stack Software EngineerCommented:
Path is declared in System.Windows.Shapes as well (besides System.IO), so u need to declare the full assembly path:

startInfo.Arguments = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "currencies.txt");
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
RTSolAuthor Commented:
Works fine! thanks a lot.
0
 
RTSolAuthor Commented:
I realized that it was ambigious so I used the full assembly path - thanks!
0
 
RTSolAuthor Commented:
Hi again,

I ran into a little problem. I can open the file using notepad at the client but I can't save it back - access denied. How can I fix thsi?
If you want me to open a new question I will do so - let me know!

Best regards
RTSol
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.