简体   繁体   English

SendMessage到Notepad ++打开文件

[英]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. 我正在尝试使用SendMessage在记事本++中以编程方式打开文件,但我没有运气。
I figured that because I can drag and drop a file onto Notepad++ and it will open it, that a SendMessage would work. 我认为,因为可以将文件拖放到Notepad ++上,它将打开它,所以SendMessage可以工作。

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 : 我使用Process.Start启动Notepad ++:

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. 执行SendMessage ,它将把我的文本发送到Notepad ++的“编辑”部分,而不是打开文件。
Any help would be great. 任何帮助都会很棒。

If you simply want to open a file in Notepad++, you can just start a new Process : 如果您只想在Notepad ++中打开文件,则可以开始一个新的Process

  • set the path of the file you want to open to the Arguments property of the ProcessStartInfo class. 将您要打开的文件的路径设置为ProcessStartInfo类的Arguments属性。
  • the FileName property is set to the path of the program you want to open. FileName属性设置为要打开的程序的路径。
  • UseShellExecute and CreateNoWindow are irrelevant here, leave the default. UseShellExecuteCreateNoWindow在这里无关紧要,保留默认值。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM