从openfile对话框获取路径

Sta*_*mpy -5 .net c# wpf openfiledialog winforms

我以这种方式打开一个文件,工作正常:

var openFileDialog = new OpenFileDialog;

if (openFileDialog.ShowDialog().GetValueOrDefault())
{
    Browser.FileDoc = File.ReadAllText(openFileDialog.FileName);
}
Run Code Online (Sandbox Code Playgroud)

现在,我想获取路径并将其传递给另一个类.我怎样才能做到这一点?

Dha*_*tel 6

你可以使用下面的分期付款代码.

OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == true)
{
string filePath = ofd.FileName;
Class2 c=new Class2(filePath);
}
Run Code Online (Sandbox Code Playgroud)

假设你的另一个班级是Class2

public class Class2
{
string Path=String.Empty;
 public Class2(string _Path)
  {
    Path=_Path;
   }
}
Run Code Online (Sandbox Code Playgroud)