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

本文关键字:怎么了 工作 方法 替换 | 更新日期: 2023-09-27 18:05:13

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

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

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

string ruta = "";
foreach (var readText in
    Directory.GetFiles(@"C:'dev'vsprojects'MvcApplication4'MvcApplication4",
    "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();

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

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

public class e{

        string ruta = "";
                foreach(var readText in Directory.GetFiles(@"C:'dev'vsprojects'MvcApplication4'MvcApplication4", "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
    }
}