English 中文(简体)
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?

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)
{

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

use flush and then close on your writer object.

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





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签