-2

I keep getting an error like when I have string, it says it cannot convert 'byte[]' to string. And when I have it as var it says the same thing but gives me an error to the data next to 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");
    }
}
Gaurang Dave
  • 3,956
  • 2
  • 15
  • 34
  • 2
    Unrelated to the actual problem (use DownloadString instead of DownloadData), but the `if (Option == Option)` looks a bit weird. – Ricardo Pieper Nov 12 '18 at 01:09
  • 1
    Be aware that `GetCurrentDirectory` can change while your program runs, and that non-admins cannot write to the application directory. Consult [Error about finding the file ...](https://stackoverflow.com/a/52546595/22437). – Dour High Arch Nov 12 '18 at 02:15

3 Answers3

0

You are trying to write string when you have a byte[], i.e., data

string data = wc.DownloadData(Option);

WebClient.DownloadData returns a byte[]

public byte[] DownloadData (string address);

Try out:

string data = wc.DownloadString (address);
Sadique
  • 22,572
  • 7
  • 65
  • 91
0

Here you go:

Replace this code string data = wc.DownloadData(Option); with System.Text.Encoding.UTF8.GetString(wc.DownloadData(Option))

The premise is that the encoding of the corresponding text is UTF-8.

Hope this would be helpful.

Lee Liu
  • 1,981
  • 1
  • 12
  • 13
0

You should use the 'DownloadString' method of WebClient to get a string:

string data = wc.DownloadString (Option);

Now the content will a string.

Poul Bak
  • 10,450
  • 5
  • 32
  • 57