-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

您在同一个文件上打开了两个 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 回答