10

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"); 
1
  • You need to make sure that the file is copied, you cannot use Process.Start for embedded resources. Check the properties and set the attribute Copy to Output Directory respectively. Commented Jun 10, 2011 at 3:13

5 Answers 5

16

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); 
Sign up to request clarification or add additional context in comments.

2 Comments

if I do this, would it work? string path = Assembly.GetExecutingAssembly().Location; Process.Start(path + "/ReadMe.txt");
If the file is in the same directory as the executable, than yes. However you better use Path.Combine instead of string concatenation. Also Location will have file name inside. You need to get directory only.
2

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.

Comments

1

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") 

2 Comments

Process.Start can open pretty much anything, it tries to use the application associated with the file.
If ProcessStartInfo.UseShellExecute is true, which is the default.
1

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

Comments

0

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"); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.