我有一個只能使用管理員權限(通過上下文菜單)運行的程序。如何製作使用本地管理員憑據運行應用程序的腳本?如何使用用戶名和密碼以管理員身份運行應用程序
我有:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//copy from
string sourcePath = @"\\10.11.11.4\Links\";
//copy to
string targetPath = @"e:\RainbowPlus\Links\";
if (!Directory.Exists(targetPath))
{
Directory.CreateDirectory(targetPath);
}
foreach (var srcPath in Directory.GetFiles(sourcePath))
{
File.Copy(srcPath, srcPath.Replace(sourcePath, targetPath), true);
}
ProcessStartInfo ps = new ProcessStartInfo();
ps.FileName = @"C:\Program Files\WinRAR\RAR.exe";
//unrar and overwrite to
ps.Arguments = @"x -o+ e:\RainbowPlus\Links\Links.rar e:\RainbowPlus\Links\";
Process.Start(ps);
}
private void button2_Click(object sender, EventArgs e)
{
Process proc = new Process
{
StartInfo =
{
CreateNoWindow = true,
UseShellExecute = true,
RedirectStandardOutput = true,
RedirectStandardInput = true,
FileName = @"E:\\RainbowPlus\\Rainbowplus.exe",
UserName = "username",
Domain = "my.domain",
Password = GetSecureString("secretpassword"),
Arguments = ""
}
};
//proc.Start();
Process p = new Process();
p.StartInfo.Verb = "runas";
System.Diagnostics.Process.Start("E:\\RainbowPlus\\Rainbowplus.exe");
}
public static SecureString GetSecureString(string str)
{
SecureString secureString = new SecureString();
foreach (char ch in str)
{
secureString.AppendChar(ch);
}
secureString.MakeReadOnly();
return secureString;
}
但我仍然有UAC PROMT。 Befor此,我用bat文件:
@echo off
REM Check runing program ####
Set ProcessName=rainbowplus.exe
TaskList /FI "ImageName EQ %ProcessName%" | Find /I "%ProcessName%"
If %ErrorLevel%==0 (echo Close rainbow program) else (goto testnetwork)
pause
exit
REM Network testing
:testnetwork
echo Testing network
pause
ping -n 4 10.11.11.4 | find "TTL=" > nul
if %errorlevel%==0 (goto copy) else (echo Network down)
pause
exit
REM Copy files
:copy
echo Updating files
pause
xcopy "\\10.11.11.4\Links" "E:\RainbowPlus\Links" /i /q /e /y /h /c
if %errorlevel%==0 (goto startprogram) else (goto error)
:startprogram
echo Succsess
pause
runas.exe /env /profile /savecred /user:"comp5234\Administrator" "E:\RainbowPlus\RainbowPlus.exe"
exit
:error
echo Files was not updated
pause
exit
哪裏是我的問題嗎?
用戶應該知道管理員憑據以管理員身份運行應用程序。如果你打算替換他們,那麼首先會否定管理員帳戶的全部要點。最好的辦法是1.要求用戶輸入憑證(最好)或2.將憑證保存到加密文件中並加載(只允許管理員更改文件)。 – TheLethalCoder
謝謝。第二個解決方案對我來說更好。 @TheLethalCoder你能幫我找到我該怎麼做的嗎? –
不,這不是一個代碼寫入服務。嘗試自己動手,然後回過頭來問問一個新的具體問題,如果你有問題,並提供一個[MCVE]。 – TheLethalCoder