Open a text file with WPF

There is a text file that I created in the root folder of the project. Now I am trying to use the Process.Start() method to run this text file externally.

The problem I got is that the path to the file is wrong and Process.Start() cannot find this text file. My code is as follows:

 Process.Start("Textfile.txt"); 

So, how should I refer to this text file correctly? Can I use a relative path instead of an absolute path? Thanks.

Edit: If I go to this code, work?

 string path = Assembly.GetExecutingAssembly().Location; Process.Start(path + "/ReadMe.txt"); 
+7
source share
5 answers

Windows should know where to find the file, so you need to somehow indicate that:

Or using the absolute path:

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

Or set the current directory:

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

Typically, CurrentDirectory set to executable.

[change]

If the file is in the same directory as the executable, you can use the following code:

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

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

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

However, if you want to enter a path, you need to use the full path. You can build the full path only by taking care of the relative path using the method specified in this post:

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

It will 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 that you add to your project called resources. If you want this to be at the root of your project, just leave the resource folder in these two lines and you will be well off.

+2
source

You will need to find out the current directory if you want to use the relative path.

 System.Envrionment.CurrentDirectory 

You can add this to your path with Path.

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

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

This scenario has been explained at the following links.

Environment.CurrentDirectory in C # .NET

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

+1
source

In the window window:

Start notepad with the file location immediately after it. WIN

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

All Articles