Open text file in notepad from C#

J

Jesper

How can I open a textfile from C# using notepad (or the
user assigned application for this).
 
M

Morten Wennevik

How can I open a textfile from C# using notepad (or the
user assigned application for this).

System.Diagnostics.Process.Start("text.txt");

You might want to add a path to text.txt. (Remember to use @"C:\Path" or
"C:\\Path")
 
S

Santiago

That will not necessarily open notepad. It will open the text file with
whatever program is associated with .txt, that's by default notepad but can
be changed (on my system it's textpad). That is probably what you want but
in the case you want notepad explicitly, use:
System.Diagnostics.Process.Start( "notepad.exe", "text.txt");

How can I open a textfile from C# using notepad (or the
user assigned application for this).

System.Diagnostics.Process.Start("text.txt");

You might want to add a path to text.txt. (Remember to use @"C:\Path" or
"C:\\Path")
 
M

Morten Wennevik

That will not necessarily open notepad. It will open the text file with
whatever program is associated with .txt, that's by default notepad but
can
be changed (on my system it's textpad). That is probably what you want
but
in the case you want notepad explicitly, use:
System.Diagnostics.Process.Start( "notepad.exe", "text.txt");
True, but he said to open the text file in notepad or whatever application
the user had assigned to text files.

Start( "notepad.exe", "text.txt" ) will most likely open notepad, but not
necessarily.
Windows doesn't keep track of where executables are kept when using
Process.Start so if notepad.exe isn't found in the directories listed in
Path it will throw an exception and fail.
You can overcome this by checking the registry to find out where a
registered application is.
HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows/CurrentVersion/App Paths
Then again, notepad isn't registered as an application so that wouldn't
work.

In the end, it's best to let the user decide and just hand it over to
windows.
 
S

Santiago

You are correct. Must have only read half the post.
I eat my shoe ;)

- Santiago

That will not necessarily open notepad. It will open the text file with
whatever program is associated with .txt, that's by default notepad but
can
be changed (on my system it's textpad). That is probably what you want
but
in the case you want notepad explicitly, use:
System.Diagnostics.Process.Start( "notepad.exe", "text.txt");
True, but he said to open the text file in notepad or whatever application
the user had assigned to text files.

Start( "notepad.exe", "text.txt" ) will most likely open notepad, but not
necessarily.
Windows doesn't keep track of where executables are kept when using
Process.Start so if notepad.exe isn't found in the directories listed in
Path it will throw an exception and fail.
You can overcome this by checking the registry to find out where a
registered application is.
HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows/CurrentVersion/App Paths
Then again, notepad isn't registered as an application so that wouldn't
work.

In the end, it's best to let the user decide and just hand it over to
windows.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top