2009-11-10 76 views
0

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

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())); 
     } 
    } 
+1

发布你的代码。 :) – Saar 2009-11-10 14:06:36

+1

你可以发布你在做什么样的代码?我不太明白......不是Console.WriteLine命令吗? – sebagomez 2009-11-10 14:07:45

+0

作为措辞的问题与该主题无关,只会询问如何写入concole窗口。请澄清。 – 2009-11-10 14:11:37

回答

9

补充一点:

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

上述

} 
catch (Exception e) 
{ 
+2

就这么简单:) +1 – RvdK 2009-11-10 14:10:40

0

使用冲洗,然后关闭刻录机的对象。

然后写完成控制台。

+1

其实,Close应该够了。但你应该考虑使用(...){...} – Guillaume 2009-11-10 14:16:40

0

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

1

不要忘记Console.ReadKey(),如果你想真正看到它的存在