Webclient.Downloadfile(Address, Filename) Doubt using variable

Asked

Viewed 164 times

1

Well, I’m creating an upgrade system, and it’s giving error in 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);
}

That mistake is as follows, when I put in line web.DownloadFile in the filename part the following code: Algoritmos 1.0.1.exe usually works. But when I use the last version variable: Algoritmos " + UltimaVersao + ".exe, throws an error.

Go to image using code Algoritmos 1.0.1.exe:

inserir a descrição da imagem aqui

Go to image using code Algoritmos " + UltimaVersao + ".exe:

inserir a descrição da imagem aqui

Why does this mistake happen? How to fix it? Can you help me? Thank you!

  • Where Ultimaversion = "1.0.1"

  • Lucas sure Ultimaversao hasn’t been changed? Try using one MessageBox.Show(UltimaVersao); or Console.Write(UltimaVersao) to make sure that the value of this variable does not have characters like line breaks, tabulations, nulls or other signs like : and ?

1 answer

2


Probably this:

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

It’s returning something you can’t imagine, you probably hope for 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 see that there should probably be non-valid characters, such as:

  • Line breaking ( n)
  • Return (\r)
  • Tabulation (\t)
  • Null (\0)
  • others as not accepted as: ? and :

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

Argumentexception: Illegal characters in path.

Looks like in the MessageBox.show that this "normal", something like 1.0.1 then it is because there must be some line break, can try removing all with replace:

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

Or else use the .Trim:

UltimaVersao = UltimaVersao.Trim();

But if you are afraid of having more invalid characters you can use Path.GetInvalidPathChars(); to clear invalid characters

Browser other questions tagged

You are not signed in. Login or sign up in order to post.