gpt4 book ai didi

c# - 替换方法不起作用 c#?这里有什么问题

转载 作者:行者123 更新时间:2023-11-30 16:19:01 30 4
gpt4 key购买 nike

我需要一个通过查找文件创建的 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();

最佳答案

不太确定您要做什么,但是,您的“替换”代码在循环之外。你需要它在里面,否则你只会替换最后一个文件的文本。

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();

}



}

编辑

刚刚也意识到这甚至无法编译。而你的类(class)叫做“e”。我对这一切有点害怕,但我还是建议使用这种格式来创建类/方法...

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

关于c# - 替换方法不起作用 c#?这里有什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15551593/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com