3

I'm using C# to write to a file located on desktop. Can I open this file (instead of double clicking on it) when closing my application (on close)?

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
   writer.Close();
   /* Some code here */
}
wonea
  • 4,783
  • 17
  • 86
  • 139
user3428151
  • 449
  • 6
  • 23
  • I don't get it. Rephrase your question please. – Sriram Sakthivel May 27 '14 at 10:31
  • 1
    What do you mean by "open the file"? as an object? process? in notepad? – Yair Nevet May 27 '14 at 10:31
  • I finished to write to a file and now want to open a text file in notepad(i do it by double click on the file name) there is a way in C# to open this file after i close a application form? – user3428151 May 27 '14 at 10:34
  • Possible duplicates: http://stackoverflow.com/questions/11365984/c-sharp-open-file-with-default-application-and-parameters, http://stackoverflow.com/questions/12294653/open-a-text-file-in-c-sharp, http://stackoverflow.com/questions/5017221/c-sharp-open-file-with-associated-application-passing-arguments?lq=1 – chiccodoro May 27 '14 at 10:36
  • @user3428151 Please add your clarification to your question in order to be more accurate. – Yair Nevet May 27 '14 at 10:50

4 Answers4

5

According to the OP's clarification comment, here it is, open the file in a Notepad process:

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

UPDATE:

Make sure to "kill" the "parent" process right after opening the Notepad process, otherwise it will hangs as @Kilazur commented-out:

Process.GetCurrentProcess().Kill(); // or Application.Exit(); or anything else
Yair Nevet
  • 12,725
  • 14
  • 66
  • 108
  • Worth nothing mentioning that when you open your file like that, your program will hang if you try to close it (the program). At least I am experiencing it, and yeah, that's not really related to the OP. – Kilazur May 27 '14 at 10:39
  • good catch! Stupidly obvious, but that's how geniuses were made before the invention of soap and stuff. – Kilazur May 27 '14 at 10:46
  • Actually I doubt that you really have to kill the process. Going to try and I will comment again. – Thorsten Dittmar May 27 '14 at 11:01
  • I've just tried it and the application *doesn't* hang in my case. See my answer - I'm going to edit right now. – Thorsten Dittmar May 27 '14 at 11:03
4

You could use Process.Start to start the default application for the file type. The clue is to use ProcessStartInfo the right way. Pass in the file path and name as "application" and set UseShellExecute to true.

I tried the following code:

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
    ProcessStartInfo si = new ProcessStartInfo();
    si.FileName = @"C:\somefile.txt";
    si.UseShellExecute = true;

    Process.Start(si);
}

This starts Notepad (the default application) with the given file opened and after that ends the application.

Please note that this does not necessarily start Nodepad, but the default application for *.txt files, which may be what you want, as you said you want the same behaviour as if you had double-clicked the file.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
1

Not sure what you mean with 'file', but you can open a document with its default application as follows:

System.Diagnostics.Process.Start(@"path to file");

Note: it will use the default application, which is normally notepad. If the user changes the default application to something else, it will use the other application. Which is a good approach (I would not explicitly call notepad, as the user might have another favorite editor like notepad++).

Also see Start a new process and Killing the current process.

Community
  • 1
  • 1
L-Four
  • 13,345
  • 9
  • 65
  • 109
1

Not sure exactly what you want here but trying to guess:

  1. If you want to write to a file in a background process, use streamwriter is the right way to go.

    using (StreamWriter sw = new StreamWriter(filepath)) { //write contents. }

  2. If you're trying to launch a file to user's desktop, you may need to create a new process to call the file's associated application.

Lee
  • 2,874
  • 3
  • 27
  • 51