0

I have to enter the file path in a text box of windows popup

I have tried a piece of the code which can return the handle of a window based on its name.

class Program
{
    private const int WM_GETTEXT = 13;
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string lclassName, string windowTitle);

    [DllImport("user32.dll")]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    static void  Main(string[] args)
    {
        IntPtr Hwnd = FindWindow(null, "Choose File to Upload");
        // Alloc memory for the buffer that recieves the text
        IntPtr Handle = Marshal.AllocHGlobal(100);
        // send WM_GWTTEXT message to the notepad window
        int NumText = (int)SendMessage(Hwnd, WM_GETTEXT, (IntPtr)50, Handle);
        // copy the characters from the unmanaged memory to a managed string
        string Text = Marshal.PtrToStringUni(Handle);

    }

my pop looks like: Windows pop up

I need to set data in a text box of the window.

  • If you're trying to upload a file from a client application there are much better ways of doing it - is there a specific reason you want to try and use the IE upload dialog? – Diado May 28 '19 at 14:53
  • What type of application is this (I'm assuming Winform)? – Kevin May 28 '19 at 14:58
  • @kevin: pop is generated from a button click on IE – vaibhav Mishra May 28 '19 at 15:09
  • @Diado: Please suggest other ways so that I can try that. – vaibhav Mishra May 28 '19 at 15:09
  • 1
    Can you expand on the exact nature of the problem that you're trying to solve? Are you trying to automatically upload a specific file from a WinForms app to a web server? It's not overly obvious from your question. – Diado May 28 '19 at 15:19
  • @Diado: I need to upload a file on the server for that we have access to a webpage. I need to automate this step. – vaibhav Mishra May 28 '19 at 15:21
  • @Diado: I have tried HTTP POST request in the past but was kind of troublesome. – vaibhav Mishra May 28 '19 at 15:32
  • You cannot set the file name value from a web page, it would lend itself to attacks by automatically uploading files. – Kevin May 29 '19 at 00:06
  • Worth looking into using `HttpClient` then. This might be a good starting place: https://stackoverflow.com/questions/16416601/c-sharp-httpclient-4-5-multipart-form-data-upload – Diado May 29 '19 at 07:41

0 Answers0