private void browsebtn_Click(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "Select Song";
fdlg.InitialDirectory = @"c:\";
fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
fdlg.FilterIndex = 2;
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
filedialoglbl.Text = fdlg.FileName;
}
}
private void runbtn_Click(object sender, EventArgs e)
{
var Path = fdlg.FileName;
var pi = new System.Diagnostics.ProcessStartInfo(Path)
{
Arguments = Path.GetFileName(Path),
UseShellExecute = true,
WorkingDirectory = Path.GetDirectoryName(Path),
FileName = "C:\\relax.exe",
Verb = "OPEN"
};
System.Diagnostics.Process.Start(pi);
}
}
如您所见,我正在尝试选择一个文件并使用该文件运行另一个程序,但我不想使用Windows默认值。
我的错误在
var Path = fdlg.FileName;
问题是fdlg在同一上下文中不是,我需要使用之前选择的目录,有关如何执行此操作的任何想法?
根据我尝试过的答案:
private OpenFileDialog fdlg = new OpenFileDialog();
private void browsebtn_Click(object sender, EventArgs e)
{
fdlg.Title = "Select Song";
fdlg.InitialDirectory = @"c:\";
fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
fdlg.FilterIndex = 2;
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
filedialoglbl.Text = fdlg.FileName;
}
}
private void runbtn_Click(object sender, EventArgs e)
{
var Path = fdlg.Text;
var pi = new System.Diagnostics.ProcessStartInfo(Path)
{
Arguments = Path.GetFileName(Path),
UseShellExecute = true,
WorkingDirectory = Path.GetDirectoryName(Path),
FileName = "C:\\relax.exe",
Verb = "OPEN"
};
System.Diagnostics.Process.Start(pi);
}
,我收到一个错误消息,说“ OpenFileDialog”没有针对“ Text”的定义或扩展方法,并且在类中包含以下行:
private OpenFileDialog fdlg = new OpenFileDialog();
我收到一个新错误,说“ GetFileName”和“ GetDirectoryName”在“字符串”中没有定义或扩展方法。
编辑
我重命名了目录的路径,但存在相同的错误
private void runbtn_Click(object sender, EventArgs e)
{
var directory =filedialoglbl.Text;
var pi = new System.Diagnostics.ProcessStartInfo(directory)
{
Arguments = directory.GetFileName(directory),
UseShellExecute = true,
WorkingDirectory = directory.GetDirectoryName(directory),
FileName = "C:\\relax.exe",
Verb = "OPEN"
};
System.Diagnostics.Process.Start(pi);
}
答案 0 :(得分:2)
您仅在FileName
中使用runbtn_Click方法中的对话框。您将其保存到名为filedialoglbl.Text
的标签中。使用它。
private void browsebtn_Click(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "Select Song";
fdlg.InitialDirectory = @"c:\";
fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
fdlg.FilterIndex = 2;
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
filedialoglbl.Text = fdlg.FileName;
}
}
private void runbtn_Click(object sender, EventArgs e)
{
var path = filedialoglbl.Text;
var pi = new System.Diagnostics.ProcessStartInfo(path)
{
Arguments = Path.GetFileName(path),
UseShellExecute = true,
WorkingDirectory = Path.GetDirectoryName(path),
FileName = "C:\\relax.exe",
Verb = "OPEN"
};
System.Diagnostics.Process.Start(pi);
}
修改
您不能将Path大写使用,因为您正在破坏Path类,并且不能使用它们的方法。
答案 1 :(得分:1)
将方法中的OpenFileDialog fdlg
声明为类成员(字段),例如,private OpenFileDialog fdlg = new OpenFileDialog();
args.gn