我目前正在使用C#WPF应用程序,该应用程序如下所示。我要这样做的是,按下第一个按钮时,基本上会打开一个浏览对话框,您可以在其中选择多个文件。我也想访问其他按钮内的那些路径。最终,我希望能够单击浏览按钮,选择一个文件,然后按第二个按钮在路径上执行功能。任何帮助将不胜感激!
private void Button_Click1(object sender, RoutedEventArgs e) //BROWSE BUTTON
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.Multiselect = true;
dlg.FileName = "Document";
dlg.DefaultExt = ".txt";
dlg.Filter = "Text documents (.txt)|*.txt";
foreach (String file in dlg.FileNames)
{
// do something
}
}
private void Button_Click(object sender, RoutedEventArgs e, string p)
{
myFunction(p);
}
您可以在Button_Click1
方法之外简单地拥有一个私有变量,该变量将保存所选的文件名。
string[] files;
private void Button_Click1(object sender, RoutedEventArgs e) //BROWSE BUTTON
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.Multiselect = true;
dlg.FileName = "Document";
dlg.DefaultExt = ".txt";
dlg.Filter = "Text documents (.txt)|*.txt";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
files = dlg.FileNames;
else
{
//Do something useful if the user cancels the dialog
}
}
然后,在其他方法中,只需引用files
变量,该变量将保存您选择的文件名数组。
用法示例:
for (int i = 0; i < files.Length; i++)
{
myFunction(files[i]);
}
上面的代码将遍历数组中的每个文件并调用该myFunction
方法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句