我需要一个通过查找文件创建的网址,可以获取该文件的网址,我得到了这个:
"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();
答案 0 :(得分:3)
不完全确定 您要尝试做什么,但是,“替换”代码不在循环之内。你需要它,否则你只会替换最后一个文件的文本。
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
}
}