Das einzige, was ich gefunden habe
Code: Select all
"scripts": {
"start": "electron src/main.js --trace-warnings --win32metadata.requested-execution-level=requireAdministrator",
"build": "electron-builder"
},
...
"win": {
"target": [
"nsis"
],
"artifactName": "webstack-deployer-for-docker-${version}.exe",
"icon": "src/assets/icons/icon.ico",
"requestedExecutionLevel": "requireAdministrator",
"adminRequested": true
},
Code: Select all
internal static void CheckElevated()
{
if (!IsRunAsAdmin())
{
RunApp();
}
}
public static void RunApp()
{
Assembly? assemblyEntries = Assembly.GetEntryAssembly();
if (assemblyEntries != null)
{
string executablePath = assemblyEntries.Location.Replace(".dll", ".exe");
if (!File.Exists(executablePath) && Environment.ProcessPath != null)
{
executablePath = Environment.ProcessPath;
}
if (File.Exists(executablePath))
{
ProcessStartInfo processInfo = new()
{
UseShellExecute = true,
WorkingDirectory = Environment.CurrentDirectory,
FileName = executablePath,
Verb = "runas"
};
try
{
Process.Start(processInfo);
Environment.Exit(0);
}
catch (Exception ex)
{
MessageBox.Show("Error: This program must be run as an administrator! \n\n" + ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("Error: This program must be run as an administrator!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
public static bool IsRunAsAdmin()
{
try
{
WindowsIdentity id = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new(id);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
catch (Exception)
{
return false;
}
}
Ich werde bald Updates zu jedem Fortschritt hinzufügen. Ich denke an den node.js is-elevated Implementierung.
Ziel ist es Nun, damit der Benutzer Verzeichnisse und Dateien programmgesteuert erstellen kann.