替换方法不工作c#?怎么了?



我需要一个通过查找文件创建的url,可以得到该文件的url我得到这个:

"C:\dev\vsprojects\MvcApplication4\MvcApplication4\hard.txt"

一切都工作得很好,问题来了,当我用代替\,但它不工作!下面是代码:

string ruta = "";
foreach (var readText in
    Directory.GetFiles(@"C:devvsprojectsMvcApplication4MvcApplication4",
    "stringCon.txt", SearchOption.AllDirectories))
{
    ruta = readText;
}
ruta = ruta.Replace(@"\", @"");
//in debugger mode says ruta parameter still having
//the \ and i cant get the content of the txt file
TextReader ReadTXT_file = new StreamReader(ruta);
//and here says that StringConexion is null, why??
string StringConexion = ReadTXT_file.ReadLine();
ReadTXT_file.Close();

我们不太确定你想做什么,但是,你的"Replace"代码在循环之外。你需要它在里面,否则你只会替换上一个文件的文本。

public class e{

        string ruta = "";
                foreach(var readText in Directory.GetFiles(@"C:devvsprojectsMvcApplication4MvcApplication4", "stringCon.txt", SearchOption.AllDirectories)) {
                    ruta = readText;
                    ruta = ruta.Replace(@"\", @"");
    //in debugger mode says ruta parameter still having the \ and i cant get the content of the txt file
                TextReader ReadTXT_file = new StreamReader(ruta);
    //and here says that StringConexion is null, why??
                string StringConexion = ReadTXT_file.ReadLine();//
                ReadTXT_file.Close(); 
                }

}

编辑

刚刚意识到这甚至不能编译。你的班级叫"e"。我有点害怕这一切,但无论如何,我将建议使用这种格式来创建类/方法…

public class MyProperClassName
{
    public void MyMethodName()
    {
        // do your file text operations here
    }
}

相关内容

最新更新