【发布时间】:2019-10-05 18:02:21
【问题描述】:
我在 CMD 中使用 attrib 命令制作了一个删除病毒快捷方式的应用程序,我需要管理员权限才能执行该命令。
目前我无法通过整个程序或流程本身来做到这一点。 下面是一段需要UAC的代码:
private void Button2_Click(object sender, EventArgs e)
{
string Disktype = null;
Disktype = ListBox1.GetItemText(ListBox1.SelectedItem); ;
if (Disktype.Contains("C:") == true)
{
MessageBox.Show("You selected C:");
}
else
{
String Message = "Cleaning" + Disk.Diskvolume + "Are you Sure?";
String Title = "Cleaning now";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
DialogResult result = MessageBox.Show(Message, Title, buttons);
if (result == DialogResult.Yes)
{
String ShowFiles = "attrib -h -r -s /s /d" + Disktype + "*.*";
String RemoveVirus = "del" + Disktype + "*.lnk";
System.Diagnostics.Process.Start("CMD.exe", ShowFiles);
System.Diagnostics.Process.Start("CMD.exe", RemoveVirus);
MessageBox.Show("Cleaning");
System.Diagnostics.Process.Start(Disktype);
Close();
}
else
{
MessageBox.Show("Cleaning done");
Close();
}
}
如您所见,我将ShowFiles 和RemoveVirus 直接设置为cmd,但我无法让它们以管理员身份运行。
整个 C# 应用程序需要它还是只需要 Process.Start 应用程序?
【问题讨论】:
-
您知道您应该使用
System.IO.Path、System.IO.File和System.IO.Drive来完成这一切。无需调用命令提示符来进行文件操作。这似乎是一个非常糟糕的主意。 -
从字面上看,您的问题由标记的副本回答。也就是说,我同意上面的说法,即你正在以错误的方式处理这一切。您应该提升您的 自己的 进程,或者甚至只是以提升的方式运行程序以开始(然后您不需要以编程方式执行任何操作),然后
System.IO.File类具有您的所有功能这里需要。
标签: c# child-process elevated-privileges