简体   繁体   English

StreamReader C#

[英]StreamReader C#

While reading a text file(which contains the location of a file to be exported to a database) using the streamReader function in C#, how can I add a confirmation message to the code that will be displayed in the command prompt window(console application) so that I know the file got read and was exported? 在使用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()));
        }
    }

Add this: 添加:

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

above 以上

}
catch (Exception e)
{

如果您想在那里实际看到它,请不要忘记Console.ReadKey()

use flush and then close on your writer object. 使用冲洗,然后关闭您的writer对象。

then write done to console. 然后将完成写入控制台。

After you read the file to the end and look for your match (assuming you have something like a boolean value to let you know the export happened and a match was found) you can check the EndOfStream property in the streamreader and output the message. 在读取完文件并查找匹配项之后(假设您具有类似布尔值的值以使您知道导出已发生并且找到了匹配项),您可以在streamreader中检查EndOfStream属性并输出消息。 Or you can just check your match value to see if it returned true. 或者,您可以仅检查您的匹配值以查看它是否返回true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM