Open text file in notepad from C#

  • Thread starter Thread starter Jesper
  • Start date Start date
J

Jesper

How can I open a textfile from C# using notepad (or the
user assigned application for this).
 
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")
 
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")
 
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.
 
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.
 
Back
Top