Write-access for C # app in its own exe dir in Windows 7

I know that user accounts in Windows 7 are limited by default, so the program cannot just write anywhere in the system (as was possible in Win XP).

But I thought it was possible that, for example, a C # application is allowed to write its own exe directory or its subfolders at least inside it (not all of these are “user settings” or should be written to “MyDocuments” ...).

So, currently my C # application is throwing a UnauthorizedAccessException when trying to write inside an exe file.

Is there anything you can do in C # code to allow writing inside an exe file?

+3
source share
4 answers

No, if the user in whom your application is running does not have write permissions to this folder, you cannot write to it. When installing the application (possibly through MSI), you can provide the necessary rights.

You can also provide a manifest file to your application.

+4
source

Is there anything you can do in C # code to allow writing inside an exe file?

Yes, but this code (which changes permissions) must be executed with administrator rights, so you are back at the beginning.

, C:\ProgramData (: Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)) .

+4

.

, /.

(, , .)

+1

, , .

, exec " " , .

WindowsPrincipal pricipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
bool hasAdministrativeRight = pricipal.IsInRole(WindowsBuiltInRole.Administrator);
if (!hasAdministrativeRight)
{
   RunElevated(Application.ExecutablePath);
   Environment.Exit(0);
}

private static void RunElevated(string fileName)
{
    ProcessStartInfo processInfo = new ProcessStartInfo();
    processInfo.Verb = "runas";
processInfo.FileName = fileName;
    try
    {
        Process.Start(processInfo);
    }
    catch (Win32Exception)
    {
        MessageBox.Show("Program needs administrator rights");
    }
}
-2

Source: https://habr.com/ru/post/1740650/


All Articles