?
Solved

Referencing text file in WPF application

Posted on 2010-01-07
6
Medium Priority
?
366 Views
Last Modified: 2013-12-12
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

0
Comment
Question by:RTSol
  • 3
  • 3
6 Comments
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 26199900
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
 
LVL 42

Accepted Solution

by:
Meir Rivkin earned 2000 total points
ID: 26199926
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
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 26199943
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Closing Comment

by:RTSol
ID: 31673991
Works fine! thanks a lot.
0
 

Author Comment

by:RTSol
ID: 26200091
I realized that it was ambigious so I used the full assembly path - thanks!
0
 

Author Comment

by:RTSol
ID: 26200663
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
An overview on how to enroll an hourly employee into the employee database and how to give them access into the clock in terminal.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question