首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

初学者求解有关问题

2013-01-05 
菜鸟求解问题有这么几个文件path1path2path3path4path5,如果path1存在则用path1,否则用path2,如果path2也

菜鸟求解问题
有这么几个文件  path1  path2  path3  path4  path5,如果path1存在则用path1,否则用path2,如果path2也不纯在则用path3,这样依次选着。
[解决办法]


            if (!System.IO.File.Exists(path1))
            {
            }
            else if (!System.IO.File.Exists(path2))
            {
            }
......

[解决办法]

string fileResult =string.Empty;
if(File.Exists(path1))
{
    fileResult =path1;
}
else if(File.Exists(path2))
{
    fileResult = path2;
}
else if(File.Exists(path3))
{
    fileResult = path3;
}
else if(File.Exists(path4))
{
    fileResult = path4;
}
else if(File.Exists(path5))
{
    fileResult = path5;
}
return fileResult;


[解决办法]
引用:
C# code?12345678910111213141516171819202122string fileResult =string.Empty;if(File.Exists(path1)){    fileResult =path1;}else if(File.Exists(path2)){    fileResult = path2;}else if(File.E……
 这样写就可以
[解决办法]
string[] paths = new string[] { path1,path2,path3,path4,path5 };
string path = "";
for (int i = 0; i < paths.Length ; i++)
{
   if (System.IO.File.Exists(paths[i]))
  {
     path = paths[i];
     break;
 }
}
使用PATH即可

热点排行