Shad0w
Shad0w

Reputation: 31

Open embedded Textfile using Notepad

I have the following problem. I want to open an embedded textfile (agb.txt) using "notepad.exe". I've got the following code:

private void linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
   string editorPath = Environment.SystemDirectory + "\\notepad.exe";
   var startInfo = new ProcessStartInfo(editorPath)
   {
      //Start Maximized
      WindowStyle = ProcessWindowStyle.Maximized,
      Arguments = "agb.txt"
   };
   //Start notepad.exe (agb.txt)
   Process.Start(startInfo);
}

When I start the program and click the Linklabel, Notpad.exe open up but can't find the embedded file (obviously). So is there a kinda 'Workaround'?

Upvotes: 3

Views: 286

Answers (2)

Peter Henell
Peter Henell

Reputation: 2466

You can start Nodepad.exe and then "send" the text from the embedded file to it. Instead of my hardcoded string, you would read the content of the file into a string and then call DoSendMessage(stringWithYourFileContent);

This question and answer is a good resource for reading the content of the embedded file: How to read embedded resource text file

class Program
{
    [DllImport("user32.dll", EntryPoint = "FindWindowEx")]
    public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

    [DllImport("User32.dll")]
    public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);



    static void Main(string[] args)
    {
        DoSendMessage("Sending a message, a message from me to you");
    }



    private static void DoSendMessage(string message)
    {
        Process notepad = Process.Start(new ProcessStartInfo("notepad.exe"));
        notepad.WaitForInputIdle();

        if (notepad != null)
        {
            IntPtr child = FindWindowEx(notepad.MainWindowHandle, new IntPtr(0), "Edit", null);
            SendMessage(child, 0x000C, 0, message);
        }
    }
}

The constant 0x000c of SendMessage is documented here http://msdn.microsoft.com/en-us/library/windows/desktop/ms632644(v=vs.85).aspx . The constant says SETTEXT which really means that the text in notepad will be replaced if you send more than one message using this constant.

Upvotes: 0

rbm
rbm

Reputation: 3253

What about saving the file to %TEMP% and then simply calling

Process.Start(@"c:\temp\agb.txt");

(this will actually open the file in whatever application is registered to load it)

Upvotes: 2

Related Questions