0

Hello my console app is for downloading html files and then sending it to a txt file, after the process is complete I would like the app to verify if the file was created and then close the program after telling the user that the page downloaded. I would like to ask how to verify if file exists and then how to exit the app.

        Console.WriteLine("Enter a Valid Webpage URL such as http://wwww.google.com, this tool will download the HTML into a .txt file");
        string webPage = Console.ReadLine();
        Uri uriResult;
        bool result = Uri.TryCreate(webPage, UriKind.Absolute, out uriResult)
            && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
        WebClient client = new WebClient();
        //DownloadsWebpage
        string reply = client.DownloadString(webPage);

        Console.WriteLine(reply);
        //Location of file here
        Console.WriteLine("Please enter a valid address to save the .txt file such as C:\\Users\\Public\\String.txt, then press any button to exit");
        string txtDir = Console.ReadLine();
        File.WriteAllText(txtDir, reply);
        Console.ReadLine();
Pro Talz
  • 3
  • 3
  • 1
    what is your question? – Dmitry Pavliv Jun 28 '18 at 17:06
  • Are you talking about closing the window when the program exits? That may be a window manager setting, "close on exit" or similar. – StackOverthrow Jun 28 '18 at 17:06
  • Thanks for sharing your plans! What's preventing you from implementing them? – trix Jun 28 '18 at 17:07
  • File.Exists() https://msdn.microsoft.com/en-us/library/system.io.file.exists(v=vs.110).aspx is a good starting point for what you are looking for – fdsafdsafdsafdsafs Jun 28 '18 at 17:10
  • Has been answered similarly here. https://stackoverflow.com/questions/5682408/command-to-close-an-application-of-console – Elijah McLain Jun 28 '18 at 17:13
  • You could replace your last `Console.ReadLine();` with: `if (File.Exists(txtDir)) Console.WriteLine("File was created successfully"); else Console.WriteLine("File creation failed"); Console.Write("\nPress any key to exit..."); Console.ReadKey();` – Rufus L Jun 28 '18 at 17:25
  • thank you all, are there any resources for capturing url exceptions and file directory exceptions? – Pro Talz Jun 28 '18 at 17:44
  • @ProTalz you can always cat any exception via a try catch block. If needed you can have multiple catch blocks that catch more specific types of exceptions but using the type `Exception` to catch all – ivcubr Jun 28 '18 at 18:42

2 Answers2

1

For verifying a file exists, there is a method called File.Exists() which would tell you if it has been created. Then once your program gets to the end of main it will just edit automatically.

Something like the following after your File.WriteAllText(txtDir, reply); may be:

if (!File.Exists(txtDir)) {
    Console.WriteLine("Some error creating file");
}

When you want to exit, just let you program close when it reaches the end of main or use Enviroment.Exit(0)

ivcubr
  • 1,988
  • 9
  • 20
  • 28
1

To check if the file Exists and exit:

if( System.IO.File.Exists(pathname))

{

 System.Environment.Exit(0);

}

// Do something else here to recover

Steve
  • 623
  • 4
  • 11