0

使用 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()));
        }
    }
4

4 回答 4

9

添加这个:

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

以上

}
catch (Exception e)
{
于 2009-11-10T14:07:01.720 回答
1

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

于 2009-11-10T14:15:30.603 回答
0

使用冲洗,然后关闭您的作家对象。

然后将完成写入控制台。

于 2009-11-10T14:07:28.890 回答
0

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

于 2009-11-10T14:09:06.000 回答