Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#字符串定义了两个文件路径_C#_Visual Studio - Fatal编程技术网

C#字符串定义了两个文件路径

C#字符串定义了两个文件路径,c#,visual-studio,C#,Visual Studio,所以我有两个文件路径,显示两个单独的图像,它们属于一起。我还为变量分配了一个字符串。例如,如果字符串表示“Tree”,则程序需要知道两个不同的路径“Desktop/tree1.png”“[…]tree2.png”。我如何才能有效地做到这一点,因为我有大约50个字符串和它们的两条路径。我认为If语句可能不是最有效的方法。有什么想法吗?谢谢如果我理解正确的话,以下几点似乎可以解决问题: string path1 = $"Desktop/{fileName}1.png"; string path2 =

所以我有两个文件路径,显示两个单独的图像,它们属于一起。我还为变量分配了一个字符串。例如,如果字符串表示“Tree”,则程序需要知道两个不同的路径“Desktop/tree1.png”“[…]tree2.png”。我如何才能有效地做到这一点,因为我有大约50个字符串和它们的两条路径。我认为If语句可能不是最有效的方法。有什么想法吗?谢谢

如果我理解正确的话,以下几点似乎可以解决问题:

string path1 = $"Desktop/{fileName}1.png";
string path2 = $"Desktop/{fileName}2.png";

您最好使用一个字符分隔路径,并且在使用路径拆分时使用单独字符,如本示例所示:

string yourPaths="path1,path2,path3,....";
string[] paths=yourPaths.split[','];
string path1=paths[0];
string path2=paths[1];
...
并在代码中使用路径

为了探索你的道路,你可以使用foreach

foreach(var path in paths){
if(desiredPath==path){} //desiredPath is your desired Path for search
else{}
}
如果您想将2个或更多路径组合在一起,可以使用StringBuilder

 StringBuilder builder = new StringBuilder();
    builder.Append(path1);
    builder.Append(path2); //path1+path2

你能再解释一下foreach搜索吗?我很抱歉成为一个Noob的例子,你想在路径中找到一条路径,foreach在所有路径中一步一步地搜索,你只需为desiredPath和foreach设置if,将你的desiredPath与所有路径进行比较