James Nelson
James Nelson

Reputation: 11

SendMessage to Notepad++ to open a file

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.

Upvotes: 1

Views: 1248

Answers (1)

Jimi
Jimi

Reputation: 32248

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();

Upvotes: 2

Related Questions