2017-12-18 8 views
1

としてのVisual Studioを実行した後に上昇を必要と私は私のWinフォームアプリケーションからOSKを実行して、サイズを変更しようとしていますが、私はこのエラーを取得しています:要求された操作をしても管理者

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は、一度に1つのインスタンスしか実行できないため、少し特殊です。したがって、osがスタートアップを処理するようにする必要があります(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スレッドが応答しなくなります。 await Task.Delay(10)を使用できるようにするには、代わりにTask.Run(async() => ...を使用するべきですが、これは別の話であり、コードを少し複雑にします。

関連する問題