简体   繁体   English

在VS(C#)中打开文件对话框时如何设置文件

[英]how to set the file when the file dialog is open in VS (c#)

I have a software that clicks the button 'Add photo': 我有一个点击“添加照片”按钮的软件:

private void OpenOpen()
        {

            var elems = webBrowser1.Document.GetElementsByTagName("input");

            foreach (HtmlElement elem in elems)
            {

                if (elem.GetAttribute("type") == "file")
                {

                    elem.InvokeMember("click");
                    break;
                }
            }

After this the file dialog window popups: choose file 之后,文件对话框窗口弹出: 选择文件

The question is: how to set the filepath through this dialogbox? 问题是:如何通过此对话框设置文件路径? The html code of the button is: 该按钮的html代码为:

<input type="file" accept="video/*,  video/x-m4v, video/webm, video/x-ms-wmv, video/x-msvideo, video/3gpp, video/flv, video/x-flv, video/mp4, video/quicktime, video/mpeg, video/ogv, image/*" name="composer_photo[]" display="inline" role="button" tabindex="0" class="_n _5f0v" id="js_56">

Thanks a lot 非常感谢

As per my understanding, you need to set a default path. 根据我的理解,您需要设置默认路径。 You could try to use the below code: 您可以尝试使用以下代码:

private void OpenOpen()
    {

        var elems = webBrowser1.Document.GetElementsByTagName("input");

        foreach (HtmlElement elem in elems)
        {

            if (elem.GetAttribute("type") == "file")
            {

                elem.SetAttribute("value", "D:\your_file.txt");
                break;
            }
        }

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

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