Creating folders within Program Files

Asked

Viewed 496 times

0

I’m having trouble creating folders within program files.

Where I work there is an application that only works if the files are inside program files and the process of creating the folders today is totally manual.

Nobody volunteered to make an installer to automate this, so since I’m a computer major, I decided to take this task even as a challenge.

The problem that I am finding is the permissioning of Windows to create folders within program files, because if it were elsewhere would be good.

I need before creating the folders have access as administrator to create the folders on the computer.

I’m using the following code:

// Verifica qual radio button está selecionado.
if (rd_golden.Checked)
{
    //Cria o diretorio para o Golden e faz o procedimentos locais
    string Golden = @"C:\Program Files\Comercial\Golden";
    if (!Directory.Exists(Golden))
    {
        Directory.CreateDirectory(Golden);
        MessageBox.Show("Diretório Criado com Sucesso!!");
    }
    else
    {
        MessageBox.Show("Diretório já Existe");
    }
}
  • Because the program only works if the files are inside program files?

  • If you are running the program with Administrator privileges you can create the folder! I don’t think there’s a way to do that automatically.

1 answer

2


You cannot just ignore permissions that are applied to folders. Also because, if you could, permissions would be useless, don’t you think?

An alternative is to run your installer with administrative privileges so you can write to the folders you want.

It is possible to force the application to run only with administrative privileges by adding a new file manifest and adding the following tag to it

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

In fact, here’s a tip: avoid using the path hardcoded this can cause problems in the future. It is possible to use Environment.SpecialFolders to return the path of this folder.

Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86);
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
  • Thank you for answering, I changed the "requireAdministrator" and appeared the message: Gravity Code Description Project File Line Delete State Error Clickonce does not support the request Execution level 'requireAdministrator'. Creatorshare

  • Oh yes. @Fabioaragão This is shown precisely for the reason described, if you are going to use Clickonce, you will need to do something to "turn around" in this.

Browser other questions tagged

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