简体   繁体   English

C#中的地址字符串的串联

[英]concatenation of address strings in C#

i am trying to concatenate an address whose 1 part is being fetched at line 15 of the following code and another part from line 19.. i want to add the address separator "\\" symbol in between both .. but i am not able to due to some error.. can anyone help me in this.. thank you..:) here is my code 我正在尝试连接一个地址,该地址的一部分将在下面的代码的第15行中被提取,而另一部分则从第19行中被获取。我想在两者之间添加地址分隔符“ \\”符号..但是我无法由于某些错误..有人可以帮我吗..谢谢.. :)这是我的代码

using System;
using System.Collections.Generic;
//using System.Linq; 
using System.Text;
using System.IO;

namespace freshtry 
{
    class Program
    {
        static void Main(string[] args)
        {
            int count = 0;
            string[] filepath= Directory.GetFiles(@"D:\project\Benten_lat\BentenPj_000_20141124_final\Testing\DPCPlus\output\msvs", "*.wav");
            string folder1 = @"D:\project\Benten_lat\BentenPj_000_20141124_final\Testing\DPCPlus\output\msvs";
            foreach(string file in filepath)
            {
                //string addr = "\";
                string filename = System.IO.Path.GetFileName(file);
                string filename1 = folder1 + "\" + filename;
                Console.WriteLine(filename1);
                //string.Concat(folder1,"\");
                count++;
            }
            //Console.WriteLine(count);
            Console.ReadLine();
        }  
    }    
}

and one more thing i don't want to add any additional directories which don't work with .net 2.0.:) 还有一件事,我不想添加无法与.net 2.0一起使用的任何其他目录。

The code here: 这里的代码:

string filename1 = folder1 + "\" + filename;

is invalid, as \\ is an escape character. 无效,因为\\是转义字符。 You can either escape the backslash, using \\\\ , or use the @ symbol to prevent escaping: @"\\" . 您可以使用\\\\来转义反斜杠,也可以使用@符号来防止转义: @"\\"

You could also look at the Path.Combine() method, which is a better way to achieve this. 您还可以查看Path.Combine()方法,这是实现此目的的更好方法。

simple answer: Use double backslash 简单答案:使用双反斜杠

string filename1 = folder1 + "\\" + filename;

Better answer: Use 更好的答案:使用

string filename1 = Path.Combine(folder1, filename);

You need to do like 你需要喜欢

 string filename1 = folder1 + "\\" + filename;

Or 要么

string filename1 = string.Concat(folder1, "\\", filename);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM