C# 完成过程后退出控制台应用程序
Hello my console应用程序用于下载html文件,然后将其发送到txt文件,完成此过程后,我希望应用程序验证文件是否已创建,然后在告诉用户页面已下载后关闭程序。我想问一下如何验证文件是否存在,然后如何退出应用程序C# 完成过程后退出控制台应用程序,c#,.net,console-application,C#,.net,Console Application,Hello my console应用程序用于下载html文件,然后将其发送到txt文件,完成此过程后,我希望应用程序验证文件是否已创建,然后在告诉用户页面已下载后关闭程序。我想问一下如何验证文件是否存在,然后如何退出应用程序 Console.WriteLine("Enter a Valid Webpage URL such as http://wwww.google.com, this tool will download the HTML into a .txt file");
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();
为了验证文件是否存在,有一个名为
file.exists()
的方法,它会告诉您是否已创建该文件。然后,一旦您的程序到达main
的末尾,它将自动进行编辑
在File.WriteAllText(txtDir,reply)之后,类似于以下内容的内容代码>可以是:
if (!File.Exists(txtDir)) {
Console.WriteLine("Some error creating file");
}
如果要退出,只需在程序到达main
的末尾时让程序关闭,或者使用environment.exit(0)
验证文件是否存在,有一个名为file.exists()
的方法可以告诉您是否已创建了该文件。然后,一旦您的程序到达main
的末尾,它将自动进行编辑
在File.WriteAllText(txtDir,reply)之后,类似于以下内容的内容代码>可以是:
if (!File.Exists(txtDir)) {
Console.WriteLine("Some error creating file");
}
要退出时,只需在程序到达main
的末尾时让程序关闭,或使用environment.exit(0)
检查文件是否存在并退出:
if(System.IO.File.Exists(路径名))
{
}
//在此处执行其他操作以恢复以检查文件是否存在并退出:
if(System.IO.File.Exists(路径名))
{
}
//在此处执行其他操作以恢复您的问题是什么?您是说在程序退出时关闭窗口吗?这可能是窗口管理器设置、“退出时关闭”或类似设置。感谢分享您的计划!是什么阻止了您实现它们呢?File.Exists()是一个很好的起点,这里也给出了类似的答案。你的问题是什么?你是说在程序退出时关闭窗口吗?这可能是窗口管理器设置、“退出时关闭”或类似设置。感谢分享您的计划!是什么阻止了您实现它们呢?File.Exists()是一个很好的起点,这里也给出了类似的答案。