1

I am trying to programmatically open a file in notepad++ using SendMessage but I'am having no luck.
I figured that because I can drag and drop a file onto Notepad++ and it will open it, that a SendMessage would work.

Declarations:

[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

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

Method:
I launch Notepad++ using Process.Start:

IntPtr cHwnd = FindWindowEx(pDocked.MainWindowHandle, IntPtr.Zero, "Scintilla", null);
SendMessage(cHwnd, WM_SETTEXT, 0, "C:\Users\nelsonj\Desktop\lic.txt");

When SendMessage executes it will send my text into the 'edit' section of Notepad++ instead of opening the file.
Any help would be great.

Jimi
  • 29,621
  • 8
  • 43
  • 61
  • 2
    As far as i know notepad++ runs normally as a single instance application. So if you start notepad++ using `Process.Start` a new tab should open. – Pretasoc Jan 10 '19 at 00:01
  • A couple of guesses: Old fashioned DDE (and, no, I don't remember how that works). The other way would be to put a file object onto the clipboard, and then send a Drag/Drop message (or sequence of messages) to Notepad++ – Flydog57 Jan 10 '19 at 00:02
  • If you do try @Pretasoc's `Process.Start` suggestion, I'm pretty sure you'll need to set `UseShellExecute` to `true` (in a `ProcessStartInfo`). But, then again... – Flydog57 Jan 10 '19 at 00:20
  • Process.Start should be the right way to go – LeY Jan 10 '19 at 01:03

1 Answers1

2

If you simply want to open a file in Notepad++, you can just start a new Process:

  • set the path of the file you want to open to the Arguments property of the ProcessStartInfo class.
  • the FileName property is set to the path of the program you want to open.
  • UseShellExecute and CreateNoWindow are irrelevant here, leave the default.

using System.Diagnostics;

Process process = new Process();
ProcessStartInfo procInfo = new ProcessStartInfo()
{
    FileName = @"C:\Program Files\Notepad++\notepad++.exe",
    Arguments = Path.Combine(Application.StartupPath, "[Some File].txt"),
};
process.StartInfo = procInfo;
process.Start();
if (process != null) process.Dispose();
Jimi
  • 29,621
  • 8
  • 43
  • 61
  • Thanks for the response, however this doesnt resolve my issue. at the start of my application i open notepad++ and embed it into my form. – James Nelson Jan 16 '19 at 00:53
  • Where in your question did you mention this or showed that you're using SetParent? BTW, I don't know why you're doing it, but that's quite a bad idea. Parenting Top Windows always ends up in tragedy. Use a RichTextBox. – Jimi Jan 16 '19 at 04:33