Creating folders within Program Files

0

I'm having trouble creating folders within program files.

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

Nobody was ready to make an installer that would automate this, so, as I graduated in computer science, I decided to take this task even as a challenge.

The problem I'm encountering is the Windows permission to create the folders inside program files, because if it were somewhere else it would be good.

I need to prioritize folders to 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");
    }
}
    
asked by anonymous 27.07.2017 / 21:04

1 answer

2

You can not simply ignore the permissions that are applied to folders. Because, if it could, the permissions would be useless, do not you think?

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

You can force the application to run only with administrative privileges by adding a new manifest file and adding the following tag to it

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

Incidentally, there is a tip: avoid using the hardcodado path that can bring problems in the future. You can use Environment.SpecialFolders to return the path of this folder.

Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86);
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
    
27.07.2017 / 21:17