Open a file with Notepad in C#

C#File

C# Problem Overview


How I open a file in c#? I don't mean reading it by textreader and readline(). I mean open it as an independent file in notepad.

C# Solutions


Solution 1 - C#

You need System.Diagnostics.Process.Start().

The simplest example:

Process.Start("notepad.exe", fileName);

More Generic Approach:

Process.Start(fileName);

The second approach is probably a better practice as this will cause the windows Shell to open up your file with it's associated editor. Additionally, if the file specified does not have an association, it'll use the Open With... dialog from windows.

Note to those in the comments, thankyou for your input. My quick n' dirty answer was slightly off, i've updated the answer to reflect the correct way.

Solution 2 - C#

You are not providing a lot of information, but assuming you want to open just any file on your computer with the application that is specified for the default handler for that filetype, you can use something like this:

var fileToOpen = "SomeFilePathHere";
var process = new Process();
process.StartInfo = new ProcessStartInfo()
{
    UseShellExecute = true,
    FileName = fileToOpen
};

process.Start();
process.WaitForExit();

The UseShellExecute parameter tells Windows to use the default program for the type of file you are opening.

The WaitForExit will cause your application to wait until the application you luanched has been closed.

Solution 3 - C#

this will open the file with the default windows program (notepad if you haven't changed it);

Process.Start(@"c:\myfile.txt")

Solution 4 - C#

System.Diagnostics.Process.Start( "notepad.exe", "text.txt");

Solution 5 - C#

You can use Process.Start, calling notepad.exe with the file as a parameter.

 Process.Start(@"notepad.exe", pathToFile);

Solution 6 - C#

Use System.Diagnostics.Process to launch an instance of Notepad.exe.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMohamedView Question on Stackoverflow
Solution 1 - C#ArenView Answer on Stackoverflow
Solution 2 - C#TimothyPView Answer on Stackoverflow
Solution 3 - C#Colin PickardView Answer on Stackoverflow
Solution 4 - C#VaibhavView Answer on Stackoverflow
Solution 5 - C#OdedView Answer on Stackoverflow
Solution 6 - C#AJ.View Answer on Stackoverflow