簡體   English   中英

如何在 C# 中將 Internet 搜索輸出打印到 Visual Studio 控制台?

[英]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)。 這將用於開發,但很可能不會用於部署。

在項目文件中,將OutputTypeWinExe更改為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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM