我想问问为什么需要管理员权限 回复主题 返回讨论区

我想问问为什么需要管理员权限?


一个查找相似图片的程序应该不需要管理员权限吧。即使你要访问需要权限的目录,也应该先询问用户再提权。

游客(楼主) 2017-12-19 04:40:18
https://docs.microsoft.com/en-us/windows/access-protection/user-account-control/how-user-account-control-works


// Win32 API
SHELLEXECUTEINFO shExInfo = {0};
shExInfo.cbSize = sizeof(shExInfo);
shExInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
shExInfo.hwnd = 0;
shExInfo.lpVerb = _T("runas");                // Operation to perform
shExInfo.lpFile = _T("C:\\MyApp.exe");       // Application to start    
shExInfo.lpParameters = "";                  // Additional parameters
shExInfo.lpDirectory = 0;
shExInfo.nShow = SW_SHOW;
shExInfo.hInstApp = 0;  


// C#
if (ShellExecuteEx(&shExInfo))
{
    WaitForSingleObject(shExInfo.hProcess, INFINITE);
    CloseHandle(shExInfo.hProcess);
}


WindowsPrincipal pricipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
bool hasAdministrativeRight = pricipal.IsInRole(WindowsBuiltInRole.Administrator);


if (!hasAdministrativeRight)
{
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.UseShellExecute = true;
    startInfo.WorkingDirectory = Environment.CurrentDirectory;
    startInfo.FileName = Application.ExecutablePath;
    startInfo.Verb = "runas";
    try
    {
        Process p = Process.Start(startInfo);
    }
    catch (System.ComponentModel.Win32Exception ex)
    {
        return;
    }


}


因为应用程序的生命周期内的权限无法改变,只能创建子进程。

PS: 可以用内存文件映射或者管道等多线程通信机制进行文件读取。
游客(沙发) 2017-12-20 09:09:40
回复主题 请注意,这里仅供讨论与新雨软件相关的问题和建议,请勿在此发表不相关的内容。
验证码: