【问题标题】:C# Reading Paths From Text File Says Path Doesn't Exist [duplicate]C#从文本文件中读取路径说路径不存在[重复]
【发布时间】:2016-01-05 20:17:17
【问题描述】:

我正在开发一个小型命令行实用程序来从目录中删除文件。用户可以选择在命令行指定路径或从文本文件中读取路径。

这是一个示例文本输入:

C:\Users\MrRobot\Desktop\Delete
C:\Users\MrRobot\Desktop\Erase
C:\Users\MrRobot\Desktop\Test

我的代码:

class Program
    {
        public static void Main(string[] args)
        {

            Console.WriteLine("Number of command line parameters = {0}", args.Length);

            if(args[0] == "-tpath:"){

                    clearPath(args[1]);
            }
            else
                if(args[0] == "-treadtxt:"){

                    readFromText(args[1]);
                }

        }

        public static void clearPath(string path)
        {
            if(Directory.Exists(path)){

                int directoryCount = Directory.GetDirectories(path).Length;

                if(directoryCount > 0){

                    DirectoryInfo di = new DirectoryInfo(path);

                    foreach (DirectoryInfo dir in di.GetDirectories())
                    {
                        dir.Delete(true); 
                    }

                }
                else{

                    Console.WriteLine("No Subdirectories to Remove");
                }

                int fileCount = Directory.GetFiles(path).Length;

                if(fileCount > 0){

                    System.IO.DirectoryInfo di = new DirectoryInfo(path);

                    foreach (FileInfo file in di.GetFiles())
                    {
                            file.Delete(); 
                    }


                }
                else{

                    Console.WriteLine("No Files to Remove");
                }

                }
            else{

                Console.WriteLine("Path Doesn't Exist {0}", path);
            }
        }

        public static void readFromText(string pathtotext)
        {
                try
                {   // Open the text file using a stream reader.
                    using (StreamReader sr = new StreamReader(pathtotext))
                    {
                        // Read the stream to a string, and write the string to the console.
                        string line = sr.ReadToEnd();
                        clearPath(line);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("The file could not be read:");
                    Console.WriteLine(e.Message);
                }

        }

    }
}

我的问题:

从文本文件中读取时,它说第一个路径不存在,并打印提示的所有路径,尽管我没有Console.WriteLine()。但是,如果我插入这些相同的路径并调用 -tPath: 它将起作用。我的问题似乎在readFromText() 我似乎无法弄清楚。

【问题讨论】:

  • 除此之外,我强烈建议您开始遵循 .NET 命名约定。

标签: c# command-line text-files streamreader


【解决方案1】:

这就是问题所在:

string line = sr.ReadToEnd();

这不是读取 数据 - 它是一次性读取整个文件。您的 clearPath 方法期望它的参数是单个路径,而不是一堆粘在一起的行。您看到所有路径的原因是 clearPath 正在被调用(所有内容都在一次调用中),没有找到“粘合在一起”的路径,它正在打印出 Console.WriteLine("Path Doesn't Exist {0}", path); 中的输入。

我怀疑你应该使用类似的东西:

var lines = File.ReadAllLines(pathtotext);
foreach (var line in lines)
{
    clearPath(pathtotext);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-07
    • 2021-07-24
    • 1970-01-01
    相关资源
    最近更新 更多