Verifying existence of shortcut

Asked

Viewed 44 times

1

I need to verify the existence of a shortcut on the desktop of the computer, but the code below does not leave the if (!File.Exists(pasta))

private void Instalando()
    {
        string pasta = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\estoque Box - Gerenciamento de estoque.Lnk";
        if (!File.Exists(pasta))
        {
            MessageBox.Show(pasta);
            pnInstalando.Visible = true;
            pnInstalando.Enabled = true;
        }
        else if (File.Exists(pasta))
        {
            MessageBox.Show("Olá");
            pbInstalando.Visible = false;
            pbInstalando.Enabled = false;
            pBarInstalando.Visible = false;
            pBarInstalando.Enabled = false;
            pbInstalado.Visible = true;
            btFechar.Enabled = true;
            btFechar.Visible = true;
        }
    }

The shortcut, even existing in the code it accuses non-existence of the shortcut. Someone can help me?

  • 1

    Not only are you looking at the file extension on the desktop?

  • The extension . Lnk is specific to shortcuts, the path is OK and the file is there, I do not know what is happening!

  • 1

    I know. What is the value of pasta? I mean, exactly, what is the value of the string? Take it by debug, or print it on the console and paste it here,

  • C:\Users\Marlon Pereira\Desktop\estoque Box - Gerenciamento de estoque.Lnk

  • 1

    Are you sure this .lnk is not duplicated in the file name?

  • 1

    Note that the extension usually gets hidden in Windows, so if you’re seeing a . lnk it’s very likely that it’s duplicated.

  • It worked, thank you!

  • 1

    Choose the young man’s answer as correct.

Show 3 more comments

1 answer

3


What is wrong is the .Lnk file name - this is duplicated.

Create the shortcut again by placing only "Stock Box - Stock Management". Then Windows itself will create a . lnk.

Or rename the file and remove the .Lnk.

Then just use:

"\\estoque Box - Gerenciamento de estoque.lnk"
  • 2

    And how do you know that the file is no longer so?

  • The link is already created, should I delete its extension? (. Lnk)

  • 1

    I did exactly like it, with the same name, windows itself adds a . lnk only that minusculo, so in case it can just take the . String Lnk, which should work.

  • @Cassioalves I edited your answer to be a little clearer, you can reverse if you don’t agree. Just let me know if you do the reversal, ok?

  • Quiet @jbueno, it’s better this way!

Browser other questions tagged

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