【问题标题】:How I can set FileName after click Upload a File from local PC?单击从本地 PC 上传文件后如何设置文件名?
【发布时间】:2017-03-29 12:32:31
【问题描述】:

我有一些短代码:

private void buttonSave_Click(object sender, EventArgs e) 
{

    OpenFileDialog openFileDialog1 = new OpenFileDialog();
    openFileDialog1.Filter = "NHC|*.nhc";
    openFileDialog1.Title = @"test.nhc";

    OpenFileDialog openfiledialog = new OpenFileDialog();
    openfiledialog.ShowHelp = true;
    openfiledialog.FileName = "test.nhc";
    openfiledialog.ShowDialog();
}

我想在OpenFileDialog 中设置文件名。
例如:我有一个 Web 应用程序,然后单击 Upload 从本地 PC 上传文件。 OpenFileDialog PopUp 确实打开了。现在我想在FileName(Windows 窗口)字段中将文件名设置为test.nhc,然后单击“打开”。

但它不起作用。

【问题讨论】:

  • 让我理解你的问题。您想,当您在 WEB 应用程序/页面上按下 UPLOAD 按钮时,选择系统拥有的打开文件对话框中显示的文件名是什么?
  • 我想从本地电脑上传文件。在布局上是按钮上传。然后我想将文件排序为 .nhc 扩展名,在 FILENAME windows 窗口中设置 test.nhc 并单击 OPEN。接下来验证新版本 .nhx 是否在 Web 应用程序上(例如由 Webdruver 使用)。
  • 这和 Zamzar.com 非常相似,当你点击 [Choose] 时,windows 会弹出对话框,然后你输入文件名并点击 Open。我想在 C# 中自动化这些步骤。
  • 这是非常简单的解决方案:Thread.Sleep(500); SendKeys.SendWait(RepoFiles.File_Name);线程.睡眠(500); SendKeys.SendWait(RepoButtons.Enter);

标签: c# fileopendialog


【解决方案1】:

您想在点击之前还是之后设置名称? 例如,您应该在 PageLoad 中定义此名称。不是点击。

OpenFileDialog openfiledialog = new OpenFileDialog();

protected void Page_Load(object sender, EventArgs e)
{    
         if (!IsPostBack)
         {
            openfiledialog.FileName = "test.nhc";
         }
}

private void buttonSave_Click(object sender, EventArgs e) 
{
          openfiledialog.ShowDialog();
}

【讨论】:

    【解决方案2】:

    您没有处理打开文件确定按钮的事件。首先,您需要创建一个 Stream 对象,然后创建一个将在 DialogResult.OK 情况下发生的事件。

    这是一个来自微软的例子

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Stream myStream = null;
                OpenFileDialog openFileDialog1 = new OpenFileDialog();
                openFileDialog1.InitialDirectory = "c:\\";
                openFileDialog1.Filter = "NHC|*.nhc";
                openFileDialog1.FilterIndex = 2;
                openFileDialog1.RestoreDirectory = true;
                openFileDialog1.Title = @"test.nhc";
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        if ((myStream = openFileDialog1.OpenFile()) != null)
                        {
                            using (myStream)
                            {
                                // Insert code to read the stream here.
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                    }
                }
            }
    
    
    
      
            }
        }

    【讨论】:

      猜你喜欢
      • 2017-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 2011-02-22
      • 1970-01-01
      • 1970-01-01
      • 2012-04-27
      相关资源
      最近更新 更多