-1

テキストファイルで特定の文字列を見つけようとしていて、その文字列の内容に基づいてフォルダーを作成しようとしています。行のどこかで、停止しただけで、例外ではなく、エラーを吐き出しません。止まるだけです。

私が見つけようとしている文字列は、次のように設定されています。

50.1 : Oxygas ------> = 1
50.2 : laser -------> = 0 
etc. 

foreach (string file in files)
        {
            string thepathoflife = Path.GetFullPath(file);
            //CreatetheFolder(file)
            string filetocopy = file;
            object bob = file.Clone();
            string bobby = bob.ToString();
            string location = file;
            bool b = false;
            string extension = Path.GetExtension(file);
            string thenameofdoom = Path.GetFileNameWithoutExtension(file);
            string filename = Path.GetFileName(file);
            ////bobby.Move(@"\\TEST12CVG\Public\Posts\Temporaryjunk" + filename);
            // string oldlocation = filename+extension;

            if (extension == ".pst" ||
              extension == ".tec" ||
              extension == ".pas" ||
              extension == ".snc" ||
              extension == ".cst" ||
              extension == ".xml")
            {
                b = true;
            }

            if (thenameofdoom == "Plasma" ||
              thenameofdoom == "Oxygas" ||
              thenameofdoom == "plasma" ||
              thenameofdoom == "oxygas" ||
              thenameofdoom == "Oxyfuel" ||
              thenameofdoom == "oxyfuel")
            {
                b = false;
            }


            if (b == true)
            // System.IO.File.WriteAllText(newlocation, bobby);
            {
                bool plasma = false;
                bool oxygas = false;
                bool punch = false;
                bool laser = false;
                var findLevel = 6;
                var path = @thepathoflife;
                var levels = path.Split(Path.DirectorySeparatorChar);
                var second = levels.Length > findLevel ? levels[findLevel] : null; 



     //this is where the problem starts.
 StreamReader s = new StreamReader(@thepathoflife);

                StreamReader st = new StreamReader(@thepathoflife);
                string currentLine;
                string searchString = "50.2 :";
                bool foundText = false;
                string searchStringab = "= 1";
                bool foundTextab = false;

                do
                {
                    currentLine = st.ReadLine();
                    if (currentLine != null)
                    {
                        foundText = currentLine.Contains(searchString);
                        foundTextab = currentLine.Contains(searchStringab);
                    }
                }
                while (currentLine != null && !foundText || currentLine != null && !foundTextab);

                if (foundText||foundTextab)
                {
                    plasma = true; //do something
                }
4

3 に答える 3

2

次の方法で簡単にできると思います。

foreach (var currentLine in File.ReadLines(thepathoflife))
{
    foundText = currentLine.Contains(searchString);
    foundTextab = currentLine.Contains(searchStringab);
    if (foundText || foundTextab)
        break;
}
于 2012-04-30T18:00:28.093 に答える
1

最初の 1 つを閉じずに、同じファイルで 2 つの StreamReader を開きました:

StreamReader s = new StreamReader(@thepathoflife);
StreamReader st = new StreamReader(@thepathoflife);

最後に、それらのいずれも破棄しませんでした。そのusingような間違いを防ぐために使用します。

using(StreamReader st = new StreamReader(@thepathoflife))
{
 do stuff;
}
于 2012-04-30T18:00:49.703 に答える
-1

すべてのコードを try catch ブロックで囲み、コンソールに例外を吐き出します (コンソール プロジェクトを使用していると想定しています)。

try{, your code...
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
于 2012-04-30T18:01:56.907 に答える