【问题标题】:c# Get File Namec# 获取文件名
【发布时间】:2013-10-14 23:38:43
【问题描述】:
private void button1_Click(object sender, EventArgs e)
    {


        OpenFileDialog newOpen = new OpenFileDialog();

       DialogResult result = newOpen.ShowDialog();

       this.textBox1.Text = result + "";

    }

它只是返回“OK”

我做错了什么?我希望获取文件的 PATH 并将其显示在文本框中。

【问题讨论】:

    标签: c# string filenames


    【解决方案1】:

    ShowDialog 方法返回用户按下的是OK 还是Cancel。这是有用的信息,但实际文件名作为属性存储在对话框中

    private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog newOpen = new OpenFileDialog();
            DialogResult result = newOpen.ShowDialog();
            if(result == DialogResult.OK) {
                  this.textBox1.Text = newOpen.FileName;
            }
        }
    

    【讨论】:

      【解决方案2】:

      您需要访问文件名:

       string filename = newOpen.FileName;
      

      或文件名,如果您允许选择多个文件:

      newOpen.FileNames;
      

      参考:OpenFileDialog Class

      private void button1_Click(object sender, System.EventArgs e) {
          Stream myStream = null;
          OpenFileDialog openFileDialog1 = new OpenFileDialog();
      
          openFileDialog1.InitialDirectory = "c:\\" ;
          openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
          openFileDialog1.FilterIndex = 2 ;
          openFileDialog1.RestoreDirectory = true ;
      
          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. Error: " + ex.Message);
              }
          } 
      }
      

      【讨论】:

        【解决方案3】:

        您需要阅读OpenFileDialog 实例的FileName 属性。这将为您提供所选文件的路径。

        【讨论】:

          【解决方案4】:

          这是一个使用现有文件作为默认文件并取回新文件的示例:

          private string open(string oldFile)
              {
                  OpenFileDialog newOpen = new OpenFileDialog();
                  if (!string.IsNullOrEmpty(oldFile))
                  {
                    newOpen.InitialDirectory = Path.GetDirectoryName(oldFile);
                    newOpen.FileName = Path.GetFileName(oldFile);
                  }
          
                  newOpen.Filter = "eXtensible Markup Language File  (*.xml) |*.xml"; //Optional filter
                  DialogResult result = newOpen.ShowDialog();
                  if(result == DialogResult.OK) {
                        return newOpen.FileName;
                  }
          
                  return string.Empty;
              }
          

          Path.GetDirectoryName(file) : 返回路径

          Path.GetFileName(file) : 返回文件名

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-07-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多