即使以管理员身份运行visual studio,请求的操作也需要提升

问题描述 投票:2回答:1

我试图从我的winform应用程序运行并调整OSK大小,但我收到此错误:

请求的操作需要提升。

我以管理员身份运行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"); 

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

c# .net winforms
1个回答
1
投票

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

(...)工作正常,但它不会让我调整键盘大小

只要确保process.MainWindowHandle不是IntPtr.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),但这是一个不同的故事,并使代码稍微复杂化。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.