简体   繁体   English

设置Windows对话框输入

[英]Set Windows Dialog Box Input

I'm trying to write a C# code that handles file uploading automaticly. 我正在尝试编写一个C#代码来自动处理文件上传。 The follow I need to implement is to choose a file from an open file dialog: 我需要实现的步骤是从打开的文件对话框中选择一个文件:

I managed to find the window using users32.dll FindWindow() method. 我设法使用users32.dll FindWindow()方法找到了窗口。 But i have no idea how to set the input if the dialog and approve the chosen file (choose a file & press OK). 但我不知道如何在对话框中设置输入并批准所选文件(选择文件并按OK)。

My Code so far: 到目前为止,我的代码:

[DllImport("user32.dll")]
public static extern int FindWindow(string lpClassName, string lpWindowName);

public const int WM_SYSCOMMAND = 0x0112;
public const int SC_CLOSE = 0xF060;
public const int WM_SETTEXT = 0x000C;

private void ChooseFile()
{
    // retrieve the handler of the window  
    int iHandle = FindWindow("#32770", "File Upload");
    if (iHandle > 0)
    {
        //Choose File
        //Press OK
    }
}

Any help will be much appreciated. 任何帮助都感激不尽。

您有C#类OpenFileDialog(http://www.dotnetperls.com/openfiledialog),对于user32.dll来说不是必需的。

What you are trying to do do is a bit odd. 您尝试做的事情有点奇怪。 You are calling Win32 function, but all you need is to use OpenFileDialog class, which is proper .NET way here ( MSDN OpenFileDialog ) 您正在调用Win32函数,但是您所需要的只是使用OpenFileDialog类,这是此处的.NET正确方法( MSDN OpenFileDialog

OpenFileDialog dlg = new OpenFileDialog();
DialogResult res = dlg.ShowDialog();
if (res == DialogResult.OK)
{
    string filePath = dlg.FileName;
    // do your upload logic here
}

After allot of research i found the solution. 经过研究,我找到了解决方案。

I used Windodows.Form.SendKeys class which simulates the keyboard and send the string to the focused window. 我使用了Windodows.Form.SendKeys类,该类模拟键盘并将字符串发送到焦点窗口。

here is the code: 这是代码:

 SendKeys.SendWait(fileInfo.FullName);
 Thread.Sleep(2000);
 SendKeys.SendWait("{ENTER}");
 Thread.Sleep(5000);

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

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