tags:

views:

154

answers:

6

hi, can anyone tell me 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 ?? thanks in advance....

thanks to you all for your effort

+6  A: 

You need 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.

Aren
I would do some escaping if I were you.
Albin Sunnanbo
I agree that this is one way to do this, another way if you wanted to open the document but not run the program would be to use something along the lines of: richTextBox1.LoadFile(Program.editInC,RichTextBoxStreamType.UnicodePlainText) for loading the actual contents into a file.
Jim
It's better to pass the file name as a second parameter. Actually, according to the [documentation](http://msdn.microsoft.com/en-us/library/53ezey2s.aspx), your code shouldn't work, as the single parameter of `Process.Start` is document or application file name, whereas yours is the application name combined with the command line parameter.
Vlad
@Albin: Thanks, totally overlooked that one :p @Jim: I believe the question was asking how to launch an editor. @Vlad: Thanks, you are correct.
Aren
+6  A: 

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

Process.Start(@"c:\myfile.txt")
Colin Pickard
I would do some escaping if I were you.
Albin Sunnanbo
you're right - fixed.
Colin Pickard
+6  A: 

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

 Process.Start(@"notepad.exe", pathToFile);
Oded
Newlines are not very useful in Process.Start...
Albin Sunnanbo
@Albin Sunnanbo - thanks... good catch :)
Oded
By the way, %pathVariables% do not work with this method. `Process.Start(@"%windir%\notepad.exe");` throws a Win32Exception:"Cannot find file" but normally it should work.
Aren
+1  A: 
System.Diagnostics.Process.Start( "notepad.exe", "text.txt");
vaibhav
+1  A: 

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

AJ
+4  A: 

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.

TimothyP