2011-04-19 80 views
12

我的應用使用ClickOnce tehcnology。今天我需要以管理員身份運行它。我修改清單文件從以管理員身份運行:requireAdministrator&ClickOnce +模擬系統時間

<requestedExecutionLevel level="asInvoker" uiAccess="false" /> 

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> 

然而VS不能編譯項目:

錯誤35的ClickOnce不支持請求執行水平 'requireAdministrator'。

我認爲不可能一次使用它們。不是嗎?我需要更改系統時間,我可以在應用程序級別執行此操作嗎?我可以模仿它,所以應用程序。可以做我想做的事。我改變時間+2小時,然後放回一秒。我有幾個DLL,他們要求時間。

回答

6

時間是一個系統範圍內的事情,你不能僅僅爲了你的過程而改變它。對你的依賴關係說謊的唯一方法是使用Detours或類似的東西來掛鉤API。如果您是低用戶帳戶,則不允許。

修改時間需要「更改系統時間」和/或「更改時區」權限(通常會給出管理員帳戶)。

正如@Chris所述,admin和ClickOnce不兼容。

+0

那麼您將如何安裝需要以管理員身份運行的應用程序? – Igor 2012-05-14 21:28:25

+0

我已經成功地運行了一個ClickOnce應用程序,它需要管理員權限,方法是首先以域管理員用戶身份登錄,然後運行clickOnce應用程序 – JoelFan 2014-09-17 19:13:13

5

正確 - ClickOnce不具有管理員權限的操作員。事實上,它的設計不是。

21

實際上,您不能使用管理權限運行ClickOnce應用程序,但有一點小問題,您可以使用管理員權限啓動新進程。 在App_Startup:

if (!IsRunAsAdministrator()) 
{ 
    var processInfo = new ProcessStartInfo(Assembly.GetExecutingAssembly().CodeBase); 

    // The following properties run the new process as administrator 
    processInfo.UseShellExecute = true; 
    processInfo.Verb = "runas"; 

    // Start the new process 
    try 
    { 
    Process.Start(processInfo); 
    } 
    catch (Exception) 
    { 
    // The user did not allow the application to run as administrator 
    MessageBox.Show("Sorry, this application must be run as Administrator."); 
    } 

    // Shut down the current process 
    Application.Current.Shutdown(); 
} 

private bool IsRunAsAdministrator() 
{ 
    var wi = WindowsIdentity.GetCurrent(); 
    var wp = new WindowsPrincipal(wi); 

    return wp.IsInRole(WindowsBuiltInRole.Administrator); 
} 

Read full article.

但是如果你想要更多的本地和簡單的解決方案只是要求用戶運行Internet Explorer作爲管理員,ClickOnce的工具也將具有管理員權限運行。

+0

這是一個很好的解決方法並且可行!但是你不能再讀取清單文件,因爲你沒有使用.application,你會使用.exe本身。 – 2016-07-06 18:15:46

+0

好東西。感謝分享。 – SamekaTV 2016-07-12 14:36:54

+0

男人,你讓我的這一天成真。儘管起初我犯了一個錯誤,那就是不檢查我的應用程序是否以管理員身份運行,所以它只是在循環中反覆打開相同的可執行文件。 – 2017-07-05 14:13:22

1

如果您從IE啓動ClickOnce應用程序,要具有管理權限,只需使用管理權限運行IE並且您的應用程序也會擁有它。

4
private void Form1_Load(object sender, EventArgs e) 
    { 
     if (WindowsIdentity.GetCurrent().Owner == WindowsIdentity.GetCurrent().User) // Check for Admin privileges 
     { 
      try 
      { 
       this.Visible = false; 
       ProcessStartInfo info = new ProcessStartInfo(Application.ExecutablePath); // my own .exe 
       info.UseShellExecute = true; 
       info.Verb = "runas"; // invoke UAC prompt 
       Process.Start(info); 
      } 
      catch (Win32Exception ex) 
      { 
       if (ex.NativeErrorCode == 1223) //The operation was canceled by the user. 
       { 
        MessageBox.Show("Why did you not selected Yes?"); 
        Application.Exit(); 
       } 
       else 
        throw new Exception("Something went wrong :-("); 
      } 
      Application.Exit(); 
     } 
     else 
     { 
      // MessageBox.Show("I have admin privileges :-)"); 
     } 
    } 
相關問題