3

ファイルを見つけて作成された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();
4

1 に答える 1

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
    }
}
于 2013-03-21T15:27:08.687 に答える