3

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'?

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Shad0w
  • 31
  • 2
  • 1
    What did you tried so far? Did you think about extracting the embedded resource into a temp folder? – Alex H Sep 25 '15 at 07:00
  • @AlexH, I don't want to extract anything. I just want to start it right from the embedded source. Maby there is another solution without notepad.exe? –  Sep 25 '15 at 07:21
  • 1
    @Shad0w because notepad.exe can only open files ( i think ) you have to extract it. You can use the 'temp'-Folder and delete the file after closing notepad. – Alex H Sep 25 '15 at 08:00
  • Surely you need to put the path of agb.txt. Notepad is going to default to the Windows system directory and it's pretty unlikely your file is located there? – Rob Sedgwick Sep 25 '15 at 08:22

2 Answers2

2

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)

rbm
  • 3,243
  • 2
  • 17
  • 28
0

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.

Community
  • 1
  • 1
Peter Henell
  • 2,416
  • 1
  • 17
  • 24