woodykiddy
woodykiddy

Reputation: 6455

Open a text file with WPF

There is a text file that I have created in my project root folder. Now, I am trying to use Process.Start() method to externally launch that text file.

The problem I have got here is that the file path is incorrect and Process.Start() can't find this text file. My code is as follows:

Process.Start("Textfile.txt");

So how should I correctly reference to that text file? Can I use the relative path instead of the absolute path? Thanks.

Edit: If I change above code to this, would it work?

string path = Assembly.GetExecutingAssembly().Location;
Process.Start(path + "/ReadMe.txt");

Upvotes: 10

Views: 23853

Answers (5)

blang32
blang32

Reputation: 1

On a windows box:

Start notepad with the file's location immediately following it. WIN

process.start("notepad C:\Full\Directory\To\File\FileName.txt");

Upvotes: 0

Alex Aza
Alex Aza

Reputation: 78457

Windows needs to know where to find the file, so you need somehow specify that:

Either using absolute path:

Process.Start("C:\\1.txt");

Or set current directory:

Environment.CurrentDirectory = "C:\\";
Process.Start("1.txt");

Normally CurrentDirectory is set to the location of the executable.

[Edit]

If the file is in the same directory where executable is you can use the code like this:

var directory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
var file = Path.Combine(directory, "1.txt");
Process.Start(file);

Upvotes: 16

IAmTimCorey
IAmTimCorey

Reputation: 16757

The way you are doing this is fine. This will find the text file that is in the same directory as your exe and it will open it with the default application (probably notepad.exe). Here are more examples of how to do this:

http://www.dotnetperls.com/process-start

However, if you want to put a path in, you have to use the full path. You can build the full path while only caring about the relative path using the method listed in this post:

http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/e763ae8c-1284-43fe-9e55-4b36f8780f1c

It would look something like this:

string pathPrefix;

if(System.Diagnostics.Debugger.IsAttached())
{
    pathPrefix = System.IO.Path.GetFullPath(Application.StartupPath + "\..\..\resources\");
}
else
{
    pathPrefix = Application.StartupPath + "\resources\";
}

Process.Start(pathPrefix + "Textfile.txt");

This is for opening a file in a folder you add to your project called resources. If you want it in your project root, just drop off the resources folder in the above two strings and you will be good to go.

Upvotes: 2

Deepesh
Deepesh

Reputation: 5604

Try using Application.StartupPath path as default path may point to current directory.

This scenario has been explained on following links..

Environment.CurrentDirectory in C#.NET

http://start-coding.blogspot.com/2008/12/applicationstartuppath.html

Upvotes: 1

Adam Driscoll
Adam Driscoll

Reputation: 9483

You'll need to know the current directory if you want to use a relative path.

System.Envrionment.CurrentDirectory 

You could append that to your path with Path

System.IO.Path.Combine(System.Envrionment.CurrentDirectory, "Textfile.txt")

Upvotes: 1

Related Questions