如何在字符串变量ex中传递路径名:sfilename包含带文件名的路径 [英] How to pass path name in string variable ex: sfilename contain path with file name
问题描述
OpenFileDialog choofdlog = new OpenFileDialog();
choofdlog.Filter =所有文件(* .DAT)| * .DAT;
choofdlog.FilterIndex = 1 ;
choofdlog.Multiselect = true;
string sFileName;
if(choofdlog.ShowDialog()= = DialogResult.OK)
{
sFileName = choofdlog.FileName;
}
FileStream fs = new FileStream(sFileName,FileMode.Open,FileAccess.Read);
如果我把sFileName这个给出错误,这样sFileName
它通过当前文件夹名称
请帮帮我
谢谢提前
Ajith
我尝试过:
如果我把sFileName这个给出错误,这样sFileName
它传递当前文件夹名
OpenFileDialog choofdlog = new OpenFileDialog();
choofdlog.Filter = "All Files (*.DAT)|*.DAT";
choofdlog.FilterIndex = 1;
choofdlog.Multiselect = true;
string sFileName;
if (choofdlog.ShowDialog() == DialogResult.OK)
{
sFileName = choofdlog.FileName;
}
FileStream fs = new FileStream(sFileName, FileMode.Open, FileAccess.Read);
this give error if I put sFileName this,this way "sFileName"
it pass current folder name
Please help me
Thanks in advance
Ajith
What I have tried:
this give error if I put sFileName this,this way "sFileName"
it pass current folder name
推荐答案
试试这个:
Try this:
OpenFileDialog choofdlog = new OpenFileDialog();
choofdlog.Filter = "All Files (*.DAT)|*.DAT";
choofdlog.FilterIndex = 1;
choofdlog.Multiselect = true;
if (choofdlog.ShowDialog() == DialogResult.OK)
{
foreach (string sFileName = choofdlog.FileNames)
{
using (FileStream fs = new FileStream(sFileName, FileMode.Open, FileAccess.Read))
{
{
...
}
}
}
当你使用multiselect时,你应该使用FileNames集合,而不是单个FileName - 你不应该试图打开文件如果用户在对话框中按取消。
When you use multiselect, you should work from the FileNames collection, not a single FileName - and you shouldn't be trying to open files if the user presses "Cancel" in the dialog.
这篇关于如何在字符串变量ex中传递路径名:sfilename包含带文件名的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!