我试图编写一个程序来导出系统信息,使用Msinfo32实用工具的按钮点击。我正在使用Powershell类在C#中这样做。现在,问题是编译后的应用程序已经设置为使用Administrator特权运行。但是,当实用程序开始保存到桌面时,我仍然会得到拒绝访问的错误。以下是“源代码”:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Management.Automation;
using System.IO;
using System.Management.Automation.Runspaces;
using System.Collections.ObjectModel;
namespace Diagnostic_Tool
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
progressBar1.Value = 10;
Runspace Run = RunspaceFactory.CreateRunspace();
Run.Open();
progressBar1.Value = 30;
Pipeline pipeline = Run.CreatePipeline();
progressBar1.Value = 50;
Command Msinfo32 = new Command("Msinfo32.exe");
string path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
Msinfo32.Parameters.Add("/nfo");
Msinfo32.Parameters.Add(path);
progressBar1.Value = 70;
pipeline.Commands.Add(Msinfo32);
pipeline.Invoke();
pipeline.Stop();
Run.Close();
progressBar1.Value = 100;
MessageBox.Show("The Task Has Completed Successfully");
}
}
}有人能告诉我出了什么事吗?
发布于 2014-09-06 17:24:25
您将被拒绝访问,因为您告诉msinfo直接将数据写入桌面目录本身,而不是写入文件。
您的“路径”变量包含桌面目录的名称。您需要将文件名附加到此参数。例:
OLD: MSinfo32.Parameters.add(path)
NEW: MSinfo32.Parameters.add(path + "\\foo.txt")https://stackoverflow.com/questions/25698195
复制相似问题