【问题标题】:StreamReader C#流读取器 C#
【发布时间】:2009-11-10 22:55:38
【问题描述】:

在使用 C# 中的 streamReader 函数读取文本文件(其中包含要导出到数据库的文件的位置)时,如何在命令提示符窗口中显示的代码中添加确认消息(控制台应用程序),以便我知道文件已被读取并已导出?

public class Script
{
    public static void Main(string[] args)
    {
        // Prepare the type that will handle all of the exporting needs
        FileExporter exporter = new FileExporter();

        try
        {
            //create an instance of StreamReader to read from a file.
            //The using statemen also closes the StreamReader.
            using (StreamReader sr = new StreamReader("ScriptFile.txt"))
            {
                string filePath;
                //read and display lines from the file until the end of
                //the file is reached.
                while ((filePath = sr.ReadLine()) != null)
                {
                    // Throw error if file does not exists to terminate the process.
                    if (!File.Exists(filePath))
                    {
                        string msg = string.Format("File not found at {0}.", filePath);
                        throw new FileNotFoundException(msg);
                    }

                    // Set the name of the export to be the name of the file.
                    string exportName = new FileInfo(filePath).Name;

                    // Export image as an SHP file if the extension matches.
                    if (filePath.Contains(".shp"))
                    {
                        exporter.processSHP(filePath, exportName, "");
                        //need confirmation that exporter.processSHP occured <<<-----***
                    }
                    else
                    {
                        string fileExtension = filePath.Split('.')[filePath.Split('.').Length - 1];

                        exporter.processIMG(filePath, exportName, "", fileExtension); 
                        //need confirmation that exporter.processIMG occured <<<-----***
                    }
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(
                string.Format("Process terminated. An error has occurred: {0}", e.ToString()));
        }
    }

【问题讨论】:

  • 你能发布你正在做什么的示例代码吗?我不太明白...... Console.WriteLine 命令不会这样做吗?
  • 措辞中的问题与主题无关,只询问如何写入concole窗口。请澄清。

标签: c# streamreader text-files


【解决方案1】:

添加这个:

Console.WriteLine("Done reading & Exporting");

上面

}
catch (Exception e)
{

【讨论】:

    【解决方案2】:

    不要忘记 Console.ReadKey() 以防你想在那里看到它

    【讨论】:

      【解决方案3】:

      使用刷新然后关闭你的 writer 对象。

      然后将完成写入控制台。

      【讨论】:

      • 其实 Close 应该就够了。但是你应该考虑一个 using(...){...}
      【解决方案4】:

      在您将文件读到最后并查找匹配项后(假设您有类似布尔值的东西来让您知道发生了导出并找到了匹配项),您可以检查流读取器中的 EndOfStream 属性并输出信息。或者你可以检查你的匹配值,看看它是否返回 true。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-21
        • 2016-04-10
        • 1970-01-01
        • 1970-01-01
        • 2013-09-21
        • 2015-09-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多