C#打开浏览器插入单词

C# open browser with inserted words

想请教各位大侠是否可以做到以下几点: 在文本框中输入类似这样的内容 "search pluto" 然后它必须搜索最后一个词。

我就是这样做的,但它不起作用,因为当我这样做时 我的浏览器打开了两次。
一个带有“https://www.google.be/#q=” 另一个打开的标签是我写的词 文本框。有人可以帮我解决这个问题吗?

这是代码:

string url = "https://www.google.be/#

if (inputTBX.Text.Contains("search ") == true)
{
    inputTBX.Text.Replace("search ", "");
    string URL = url += inputTBX.Text;
    Process.Start(@"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",
    URL);
    inputTBX.Clear();
}

就像在一个简单的控制台应用程序中进行测试一样,我尝试了以下操作,它可以启动我的默认浏览器

var t = "pluto";
Process.Start("http://google.com/search?q=" + t);

这也有效

var t = "pluto";
Process.Start("https://www.google.be/search?q=" + t);

在您的情况下,您需要将查询字符串设为以下

https://www.google.be/#q=pluto

你的第一个问题是你正在尝试使用 Replace 方法,但你需要将它分配到这里是你刚刚测试的代码的有效解决方案,请注意我所做的差异

inputTBX.Test = "search pluto";
string url = "https://www.google.be/search?q=";

if (inputTBX.Contains("search "))
{
    inputTBX.Text = inputTBX.Replace("search ", "");
    string URL = url += inputTBX;
    Process.Start(URL); // this will launch your default web browser
    inputTBX.Clear();
}

因为你的查询字符串中有搜索这个词..你真的不需要这一行 if (inputTBX.Contains("search ")) 但如果你保留它,如果你传递 search planet pluto 它会起作用,例如你的文本框

这部分

inputTBX.Text.Replace("search ", "");

非常糟糕,因为如果您输入这样的字符串,它将无法完成工作

"search research on Beethoven's work"

如果您希望关键短语是 "search ",您应该改为这样做

inputTBX.Text.Substring(("search ").Length); //this way it will skip the "search " phrase and use the rests of the phrase for searching

至于给定 URL 的过程,只需执行

string url = "https://www.google.be/#q="; //notice the q= is missing in your code shown
Process.Start(url + inputTBX.Text.Substring(("search ").Length));