User32 API SendMessage to open a file from open file dialogbox in Win10 OS

markandbhatt 1 Reputation point

I have written the following c# statement to open a file

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]  
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));  

The above code works for the following open file dialog box. Opens C:\devops\dev.log


The same code fails for the following open file dialog box.


I noticed that the above dialog box has "Search" text box. Will it make any difference?

After sending file path to open file dialog box, I tried to read from the dialog box to make sure the dialog box has correct file path text. But, I discovered that the output is "search this pc" and not the file path to open.

int WM_GETTEXT = 0xD;  
int textLength = SendMessage(ptr.ToInt32(), WM_GETTEXTLENGTH, 0, 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  
Windows 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
10,986 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,523 questions
{count} votes