简体   繁体   中英

How to print internet search outputs to visual studio console in C#?

So I am working on a c# program which is intended to perform a random Google Search every random time between 1 and 100 minutes. However, when running the program, I am noticing the CPU usage never changes and there is no output from the search, despite there being a command to write the results to the console. I am wondering how to print google search results to the output console in Visual Studio? Thanks.

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

I don't know about the "Visual Studio console", but I know how to make the console appear in a C# WPF program (and almost guaranteed also a WinExe program, including WinForms). This you would use for development, but most probably not deployment.

In the project file, change the OutputType from WinExe to just Exe , and also add a line with DisableWinExeOutputInference set to true . As seen in this snippet from one of my projects.

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <DisableWinExeOutputInference>true</DisableWinExeOutputInference>
    <TargetFramework>net6.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
  </PropertyGroup>

In a WinForms program you'd see UseWinForms instead of UseWPF . That's as it should be.

All of this is in .NET 5 and .NET 6 and the new project format. Maybe this is also available in .NET Core 3 , but you'll have to investigate that if it's of interest. If you're on older .NET platforms and/or project formats, then do consider updating.

The console window will appear along with your main window. You can close either window to shut down the application. If you experience shutdown code not running, let me know, and I'll go into that too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM