2014-09-06 99 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

相關問題