【问题标题】:OpenFileDialog Control - How Can I Grab the Selected Path and Show it in a Text Box?OpenFileDialog 控件 - 如何获取选定路径并将其显示在文本框中?
【发布时间】:2010-07-11 08:38:17
【问题描述】:

这是我的 sn-p:

private void btnBrowseCInv_Click(object sender, EventArgs e)
{
  ofdBrowseVInv.Title = "Locate Customer Invoice File";
  ofdBrowseVInv.Filter = "Portable Document Format (*.pdf)|*.pdf|All Files (*.*)|*.*";
  ofdBrowseVInv.FileName = "";
  ofdBrowseVInv.FilterIndex = 0;

  ofdBrowseVInv.InitialDirectory = "";

  ofdBrowseVInv.CheckFileExists = true;
  ofdBrowseVInv.CheckPathExists = true;

  if (ofdBrowseVInv.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  {
     //txtInvoicePathCInv.Text = ofdBrowseVInv... What property should i use?
  }
}

如下所示,一旦用户选择一个文件并单击打开。我希望选定的路径显示在名为“txtInvoicePathCInv”的指向文本框中。有什么想法吗?

我正在使用 Windows 应用程序...

alt text http://img708.imageshack.us/img708/54/99763211.jpg

【问题讨论】:

  • +1 样式点用于添加截图! :)

标签: c# .net vb.net winforms


【解决方案1】:

使用FileName 属性:

txtInvoicePathCInv.Text = ofdBrowseVInv.FileName;

这将为您提供整个路径,但您始终可以使用它的目录部分,使用 Path.GetDirectoryName:

txtInvoicePathCInv.Text = Path.GetDirectoryName(ofdBrowseVInv.FileName);

【讨论】:

    【解决方案2】:
    string filename = System.IO.Path.GetFileName(ofdBrowseVInv.FileName); 
    string path = System.IO.Path.GetDirectoryName(ofdBrowseVInv.FileName); 
    

    【讨论】:

      猜你喜欢
      • 2019-01-29
      • 2016-09-03
      • 1970-01-01
      • 1970-01-01
      • 2016-08-23
      • 2014-08-18
      • 1970-01-01
      • 1970-01-01
      • 2012-06-03
      相关资源
      最近更新 更多