流读取器 C#

发布于 2024-08-11 05:25:54 字数 2188 浏览 7 评论 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()));
        }
    }

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一刻暧昧 2024-08-18 05:25:54

添加此:

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

上面

}
catch (Exception e)
{

Add this:

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

above

}
catch (Exception e)
{
忘东忘西忘不掉你 2024-08-18 05:25:54

不要忘记 Console.ReadKey(),以防您想实际看到它

Don't forget the Console.ReadKey() in case you want to actually see it up there

左岸枫 2024-08-18 05:25:54

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

然后将done写入控制台。

use flush and then close on your writer object.

then write done to console.

清晨说晚安 2024-08-18 05:25:54

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

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. Or you can just check your match value to see if it returned true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文