[英]How to print internet search outputs to visual studio console in C#?
因此,我正在开发一个 c# 程序,该程序旨在在 1 到 100 分钟之间的每个随机时间执行一次随机 Google 搜索。 但是,在运行程序时,我注意到 CPU 使用率从未改变,并且搜索中没有 output,尽管有将结果写入控制台的命令。 我想知道如何将谷歌搜索结果打印到 Visual Studio 中的 output 控制台? 谢谢。
private void googlesearch_DoWork(object sender, DoWorkEventArgs e)
{
MessageBox.Show("Random google search will be conducted between 1 am and 12 pm. Times will be sporadic.");
Random random = new Random();
TimeSpan start = TimeSpan.FromHours(1);
TimeSpan end = TimeSpan.FromHours(12);
int maxMinutes = (int)((end - start).TotalMinutes);
for (int i = 0; i < 100; ++i)
{
int minutes = random.Next(maxMinutes);
TimeSpan t = start.Add(TimeSpan.FromMinutes(minutes));
// Do something with t...
string uriString = "http://www.google.com/search";
WebClient webClient = new WebClient();
Random r = new Random();
string[] words = { "man", "rat", "cow", "chicken", "confuse", "cool", "space news", "science", "holiday", "chickens", "travel", "europoe", "USA", "president", "cool stuff", "world news", "donald trump", "politics", "space", "astronomy", "radio", "cool stuff", "USA News", "tell me a funny joke", "do a barrel rool", "mario and luigi", "radio", "abcdefghijklomnopqrstuvwxyz", "popular computer games", "graphics cards", "performance", "sports news", };
Console.WriteLine(words[r.Next(0, words.Length)]);
string word = words[r.Next(0, words.Length)];
NameValueCollection nameValueCollection = new NameValueCollection();
nameValueCollection.Add("q", word);
webClient.QueryString.Add(nameValueCollection);
Console.WriteLine(uriString);
我不了解“Visual Studio 控制台”,但我知道如何使控制台出现在 C# WPF 程序中(并且几乎可以保证 WinExe 程序,包括 WinForms)。 这将用于开发,但很可能不会用于部署。
在项目文件中,将OutputType
从WinExe
更改为Exe
,并添加一行DisableWinExeOutputInference
设置为true
。 从我的一个项目的这个片段中可以看出。
<PropertyGroup>
<OutputType>Exe</OutputType>
<DisableWinExeOutputInference>true</DisableWinExeOutputInference>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWPF>true</UseWPF>
</PropertyGroup>
在WinForms
程序中,您会看到UseWinForms
而不是UseWPF
。 这是应该的。
所有这些都在.NET 5
和.NET 6
以及新的项目格式中。 也许这在.NET Core 3
中也可用,但如果你感兴趣的话,你必须调查一下。 如果您使用的是较旧的 .NET 平台和/或项目格式,请考虑更新。
控制台 window 将与您的主 window 一起出现。 您可以关闭 window 以关闭应用程序。 如果您遇到关闭代码未运行的情况,请告诉我,我也会将 go 加入其中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.