【问题标题】:Get path from openfile dialog [closed]从openfiledialog获取路径[关闭]
【发布时间】:2023-03-02 21:07:01
【问题描述】:

我以这种方式打开一个文件,效果很好:

var openFileDialog = new OpenFileDialog;

if (openFileDialog.ShowDialog().GetValueOrDefault())
{
    Browser.FileDoc = File.ReadAllText(openFileDialog.FileName);
}

现在,我想获取路径并将其传递给另一个类。我该怎么做?

【问题讨论】:

  • 您的问题能具体一点吗? “如何将其传递给另一个班级?”:什么班级?
  • Stampy,你的问题基本上是关于范围,我建议你在谷歌上进一步阅读这个主题
  • openFileDialog.FileName = 路径

标签: c# .net wpf winforms openfiledialog


【解决方案1】:

您可以使用下面提到的代码。

OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == true)
{
string filePath = ofd.FileName;
Class2 c=new Class2(filePath);
}

假设你的另一个类是 Class2 那么

public class Class2
{
string Path=String.Empty;
 public Class2(string _Path)
  {
    Path=_Path;
   }
}

【讨论】:

  • 我建议这个:public class Class2 { string _path=String.Empty; public Class2(string p) { Path=p; } public Path{get{return _path;}{set _path=value;}} }
猜你喜欢
  • 1970-01-01
  • 2010-10-01
  • 2014-08-18
  • 1970-01-01
  • 2014-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多