WebClient.DownloadFile (Address, FileName) Doubt using variable

1

Well, I'm creating an update system, and it's giving error on one part

WebClient web = new WebClient();
string DownloadVersion = web.DownloadString("https://drive.google.com/uc?authuser=0&id=1bpVdsyUj3oOn1gLbcVRE7uZjIczI5Yc_&export=download");
string UltimaVersao = DownloadVersion.Split('\n')[0];
string VersaoDessePrograma = Application.ProductVersion; 

if (Convert.ToDecimal(VersaoDessePrograma) < Convert.ToDecimal(UltimaVersao))
{
    if (MessageBox.Show("Um novo update está disponivel!" + Environment.NewLine + "Você quer atualizar?", "Atualização", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) 
    {
        web.DownloadFile(DownloadVersion.Split('\n')[1], "Algoritmos 1.0.1.exe");
        MessageBox.Show("O programa foi baixado!" + Environment.NewLine + "O aplicativo será fechado.", "Atualização", MessageBoxButtons.OK, MessageBoxIcon.Information); 
        System.Diagnostics.Process.Start(Application.StartupPath + @"\Algoritmos 1.0.1.exe");
        Close();
    }
}
else
{
    MessageBox.Show("O programa já está atualizado!", "Verificador", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

This error is as follows, when I put in the line web.DownloadFile in the part of fileName the following code: Algoritmos 1.0.1.exe works out, normally. But when I use the variable of the latest version: Algoritmos " + UltimaVersao + ".exe , it throws an error.

Go to image using code Algoritmos 1.0.1.exe :

GotoimageusingcodeAlgoritmos" + UltimaVersao + ".exe :

Why does this error occur? How to solve? Can you help me? Thanks!

    
asked by anonymous 10.12.2017 / 22:15

1 answer

1

Probably this:

 string UltimaVersao = DownloadVersion.Split('\n')[0];

Returning something you can not imagine, you probably expect something like 1.0.1 , but maybe this is returning something else, if I just did this:

MessageBox.Show(UltimaVersao);

or

Console.Write(UltimaVersao);

You will find that there should probably be invalid characters, such as:

  • Line break (\ n)
  • Return (% with%)
  • Tabulation (% with%)
  • Null (% with%)
  • others as not accepted as: \r and \t

Because to create files in the folders some characters are not allowed, which will cause the error:

  

ArgumentException: Illegal characters in path.

If it looks like ? that this "normal", something like : then it's because it should have some line break, you can try removing all with replace:

UltimaVersao = UltimaVersao.Replace(System.Environment.NewLine, "");

Or use MessageBox.show :

UltimaVersao = UltimaVersao.Trim();

But if you are afraid of having more invalid characters you can use 1.0.1 to clear invalid characters

    
11.12.2017 / 01:18