User32 API SendMessage 在 Win10 操作系统中从打开文件对话框中打开文件

问题描述

我编写了以下 c# 语句来打开文件

[DllImport("user32.dll")]
public static extern int SendMessage(int hWnd,int Msg,int wParam,StringBuilder lParam);
private const int WM_SETTEXT = 12;

private void OpenFile()
{
    var text = @"c:\devops\dev.log";
    SendMessage(ptr.ToInt32(),WM_SETTEXT,text.Length,new StringBuilder(text));
}

以上代码适用于以下打开文件对话框。打开 C:\devops\dev.log

enter image description here

以下打开文件对话框的相同代码失败。

enter image description here

我注意到上面的对话框有“搜索”文本框。会有什么不同吗?

编辑

发送文件路径以打开文件对话框后,我尝试从对话框中读取以确保对话框具有正确的文件路径文本。但是,我发现输出是“搜索这台电脑”,而不是要打开的文件路径。

int WM_GETTEXTLENGTH = 0xE;
 int WM_GETTEXT = 0xD;
 int textLength = SendMessage(ptr.ToInt32(),WM_GETTEXTLENGTH,0);
    
         if (textLength == 0)
         {
             return string.Empty;
         }
    
         var text = new StringBuilder(textLength + 1);
    
         int nRet = SendMessage(ptr.ToInt32(),WM_GETTEXT,text.Capacity,text);
         if (nRet == 0) return
             string.Empty;
         Console.WriteLine(text.ToString());

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)