我想问问为什么需要管理员权限?
一个查找相似图片的程序应该不需要管理员权限吧。即使你要访问需要权限的目录,也应该先询问用户再提权。
站长回复:1、为什么需要管理员权限?
答:有一部分用户喜欢把软件放到C:\Program Files目录下,没有管理员权限,软件的参数设置,对比的临时结果,图片的特征信息等都无法保存。
2、为什么不询问用户再提权?
答:作者能力有限,没有找到询问用户再提权的方法,您要有提供给我,万分感谢。
PS:作者提供软件不为盈利,一是喜欢开发,二是希望能帮助大家,所以您如果不放心这款软件,建议您别用,您不愉快,我也不开心。(质疑软件的安全性,我不生气,但您这口气,我不是很喜欢)
游客(楼主) 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