Open a file with Notepad in C#

前端 未结 6 593
春和景丽
春和景丽 2020-11-28 06:35

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.

6条回答
  •  孤独总比滥情好
    2020-11-28 06:49

    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.

提交回复
热议问题