[英]C# Application get runs Twice while running as an administrative
I am trying to run my app from start-up as an administrative.我正在尝试从启动时以管理员身份运行我的应用程序。 To do so I have written the below code:
为此,我编写了以下代码:
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
{
Application.Run(new HomePage());
}
else
{
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = Environment.CurrentDirectory;
startInfo.FileName = Application.ExecutablePath;
startInfo.Verb = "runas";
try
{
System.Diagnostics.Process.Start(startInfo);
Application.Exit();
}
catch
{
return;
}
Application.Exit();
}
Application.Run(new HomePage());
This is my program.cs file.这是我的 program.cs 文件。 The problem is when I run it it comes with two same application.
问题是当我运行它时它带有两个相同的应用程序。 I have tried many methods but I failed to solve it.
我尝试了很多方法,但都没有解决。
Is it possible to run my app as an admin from start-up without provoking the run as administrative dialogue?是否可以从启动时以管理员身份运行我的应用程序而不会引发以管理员身份运行的对话?
You're calling application.run twice (once Inside the if if you're an administrator, and once after the if/else block) so for an administrator it will run twice.您调用 application.run 两次(如果您是管理员,一次在 if 内部,一次在 if/else 块之后),因此对于管理员,它将运行两次。
Just remove the application.run after the if/else or invert your if else to say if not administrator (what you currently have in the else) , don't put an else and leave the application run at the end wich will only be hit if you're not administrator只需在 if/else 之后删除 application.run 或反转您的 if else 来说明 if 不是管理员(您目前在 else 中拥有的内容),不要放置 else 并让应用程序在最后运行,只会被击中如果您不是管理员
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
// If you're an administrator
if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
{
// Then run the application (first time)
Application.Run(new HomePage());
}
else
{
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = Environment.CurrentDirectory;
startInfo.FileName = Application.ExecutablePath;
startInfo.Verb = "runas";
try
{
System.Diagnostics.Process.Start(startInfo);
Application.Exit();
}
catch
{
return;
}
Application.Exit();
}
// you end up here after it ran as an admin, skipping the else and you tell it to run again, either remove that line or invert your condition
Application.Run(new HomePage());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.