software-mariodiana
10/8/2015 - 6:03 PM

C# example to open a text file in an external text editor application (in this case, Notepad).

C# example to open a text file in an external text editor application (in this case, Notepad).

// Adpated from: http://stackoverflow.com/a/17807003/155167

using System.Diagnostics;
using System.IO;

namespace OpenTextFileInNotepad
{
    class Program
    {
        public const string TextEditor = @"%windir%\system32\notepad.exe";

        static void Main(string[] args)
        {
            string textFilePath = args[0];

            ProcessStartInfo pi = new ProcessStartInfo(textFilePath);

            pi.Arguments = Path.GetFileName(textFilePath);
            pi.UseShellExecute = true;
            pi.WorkingDirectory = Path.GetDirectoryName(textFilePath);
            pi.FileName = Environment.ExpandEnvironmentVariables(TextEditor);
            pi.Verb = "OPEN";

            Process.Start(pi);
        }
    }
}