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”在“string”中没有定义或扩展方法
编辑
我将路径重命名为目录但同样的错误
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);
}
您在 runbtn_Click 方法中使用对话框仅用于FileName
. 您将其保存到名为 的标签中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 类并且您不能使用它们的方法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句