如何在字符串变量ex中传递路径名:sfilename包含带文件名的路径 [英] How to pass path name in string variable ex: sfilename contain path with file name

查看:162
本文介绍了如何在字符串变量ex中传递路径名:sfilename包含带文件名的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆