2014-09-06 54 views
0

我正在尝试编写一个程序,用于单击按钮时使用Msinfo32实用程序导出系统信息。我正在使用Powershell类在C#中完成此操作。现在,编译的应用程序已经设置为使用管理员权限运行。但是,当该实用程序开始保存到桌面时,我仍然收到拒绝访问错误。以下是源代码: -在C#中使用PowerShell运行命令时访问被拒绝

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"); 


    } 


} 
} 

任何人都可以请告诉发生了什么问题吗?

回答

1

您的访问被拒绝,因为您告诉msinfo将数据直接写入桌面目录本身,而不是写入文件。

您的'路径'变量包含桌面目录的名称。您需要为此参数附加一个文件名。例如:

OLD: MSinfo32.Parameters.add(path) 

NEW: MSinfo32.Parameters.add(path + "\\foo.txt") 
+0

: - 我试过你的方法,现在我得到“文件名,目录名称或卷标语法不正确”。能否请你帮忙? – 2014-09-07 14:26:04

+1

忘记双反斜杠 – 2014-09-07 15:22:19

+0

谢谢队友!非常感谢您的帮助。 – 2014-09-08 06:04:45

相关问题