2017-12-18 360 views
1

我试图运行,并从我的WinForm应用程序调整OSK但我收到此错误后:请求的操作需要提升,即使在运行Visual Studio作为管理员

The requested operation requires elevation.

我在运行Visual Studio为管理员。

System.Diagnostics.Process process = new System.Diagnostics.Process(); 
process.StartInfo.UseShellExecute = false; 
process.StartInfo.RedirectStandardOutput = true; 
process.StartInfo.RedirectStandardError = true; 
process.StartInfo.CreateNoWindow = true; 
process.StartInfo.FileName = "c:\\windows\\system32\\osk.exe"; 
process.StartInfo.Arguments = ""; 
process.StartInfo.WorkingDirectory = "c:\\"; 

process.Start(); // **ERROR HERE** 
process.WaitForInputIdle(); 
SetWindowPos(process.MainWindowHandle, 
this.Handle, // Parent Window 
this.Left, // Keypad Position X 
this.Top + 20, // Keypad Position Y 
panelButtons.Width, // Keypad Width 
panelButtons.Height, // Keypad Height 
SWP_SHOWWINDOW | SWP_NOZORDER); // Show Window and Place on Top 
SetForegroundWindow(process.MainWindowHandle); 

然而,

System.Diagnostics.Process.Start("osk.exe"); 

工作得很好,但它不会让我调整键盘

+0

您是否试过在“释放”模式下运行?即运行你的程序exe文件?为了以管理员身份运行你的exe,你可以使用'startInfo.Verb =“runas”;' – Sunil

回答

0

process.StartInfo.UseShellExecute = false将禁止你做你想做的事。 osk.exe有点特殊,因为一次只能运行一个实例。所以你必须让操作系统处理启动(UseShellExecute必须为真)。

(...) Works just fine but it wont let me resize the keyboard

只要确保process.MainWindowHandleIntPtr.Zero。可能需要一段时间,你不允许用process.WaitForInputIdle()询问流程实例,可能是因为proc是由os运行的。您可以轮询该句柄,然后运行您的代码。像这样:

System.Diagnostics.Process process = new System.Diagnostics.Process(); 
// process.StartInfo.UseShellExecute = false; 
// process.StartInfo.RedirectStandardOutput = true; 
// process.StartInfo.RedirectStandardError = true; 
process.StartInfo.CreateNoWindow = true; 
process.StartInfo.FileName = "c:\\windows\\system32\\osk.exe"; 
process.StartInfo.Arguments = ""; 
process.StartInfo.WorkingDirectory = "c:\\"; 

process.Start(); // **ERROR WAS HERE** 
//process.WaitForInputIdle(); 

//Wait for handle to become available 
while(process.MainWindowHandle == IntPtr.Zero) 
    Task.Delay(10).Wait(); 

SetWindowPos(process.MainWindowHandle, 
this.Handle, // Parent Window 
this.Left, // Keypad Position X 
this.Top + 20, // Keypad Position Y 
panelButtons.Width, // Keypad Width 
panelButtons.Height, // Keypad Height 
SWP_SHOWWINDOW | SWP_NOZORDER); // Show Window and Place on Top 
SetForegroundWindow(process.MainWindowHandle); 

由于注:使用Wait()(或Thread.Sleep);在WinForms中应该非常有限,它会导致ui线程无响应。您应该在这里使用Task.Run(async() => ...,以便能够使用await Task.Delay(10),但这是一个不同的故事,并使代码稍微复杂。

相关问题