循环错误,包括字符串和变量



我一直收到一个错误,比如当我有字符串时,它说它无法将"byte[]"转换为字符串。当我把它作为var时,它会说同样的话,但会给我一个Results.txt旁边的数据错误

if (Lock.Contains("Mode: Data Grabber"))
{
Console.WriteLine("Loading Data Grabber 2.0! Mode = Data Grabber!n");
Console.WriteLine("Enter the websites url!n");
Console.WriteLine("(n) " + "(n)");
if (Option == Option)
{
WebClient wc = new WebClient();
string data = wc.DownloadData(Option);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Downloading data from " + data);
Thread.Sleep(3000);
Console.WriteLine("n");
if (!File.Exists(Directory.GetCurrentDirectory() + @"/Results.txt"))
{
File.Create(Directory.GetCurrentDirectory() + @"/Results.txt");
}
File.WriteAllText(Directory.GetCurrentDirectory() + @"/Results.txt", data);
Console.WriteLine("All data has been sent to the path");
}
}

开始:

将此代码string data = wc.DownloadData(Option);替换为System.Text.Encoding.UTF8.GetString(wc.DownloadData(Option))

前提是对应文本的编码是UTF-8。

希望这会有所帮助。

您应该使用WebClient的"DownloadString"方法来获取字符串:

string data = wc.DownloadString (Option);

现在内容将是一个字符串。

当您有byte[],即data时,您正在尝试写入string

string data = wc.DownloadData(Option);

WebClient.DownloadData返回byte[]

public byte[] DownloadData (string address);

试用:

string data = wc.DownloadString (address);

最新更新