2013-04-23 64 views
1

我所要做的就是能夠從後面的代碼運行VBS腳本,但我得到這個錯誤:「系統找不到指定的文件」。我知道路徑名,我只需要執行.vbs腳本,但它給了我很多時間,我無法弄清楚。請幫忙。在這裏感謝 是我的代碼如何從C#代碼運行VBS腳本

System.Diagnostics.Process.Start(@"cscript //B //Nologo \\loc1\test\myfolder\test1.vbs"); 

我已經更新了,如下圖所示,但我得到一個安全警告問我,如果我想打開它的代碼。有沒有辦法不接受這些警告,只是在沒有任何警告的情況下運行腳本? 這裏是更新的代碼:

Process proc = null; 
     try 
     { 
      string targetDir = string.Format(@"\\loc1\test\myfolder");//this is where mybatch.bat lies 
      proc = new Process(); 
      proc.StartInfo.WorkingDirectory = targetDir; 
      proc.StartInfo.FileName = "test1.vbs"; 
      proc.StartInfo.Arguments = string.Format("10");//this is argument 
      proc.StartInfo.CreateNoWindow = false; 
      proc.Start(); 
      proc.WaitForExit(); 
     } 
     catch (Exception ex) 
     { 
      // Console.WriteLine("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString()); 
     } 

回答

2

您的代碼爲我工作很好,我認爲錯誤是你的文件路徑

更好確認您指定的文件路徑是否有效 ..

您可以運行文件中像下面還..

Process scriptProc = new Process(); 
scriptProc.StartInfo.FileName = @"cscript"; 
scriptProc.StartInfo.Arguments ="//B //Nologo \\loc1\test\myfolder\test1.vbs"; 
scriptProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 
scriptProc.Start(); 
scriptProc.WaitForExit(); 
scriptProc.Close(); 

檢查你的文件路徑你給..

+0

我認爲這工作正常,沒有問題,但如果你可以告訴我,如果我想運行位於另一臺服務器上的VBS腳本,我會如何做到這一點?我假設我必須提供用戶名和密碼,但不要命名如何修改此代碼?謝謝 – moe 2013-04-23 16:38:30

+0

@moe - 較新版本的Windows需要您授權這樣的運行腳本文件。更新您的問題以反映此信息。 – 2013-04-23 17:12:25

3

參數必須單獨包含。有一個參數字段用於將參數傳遞給進程。

您可以將此作爲執行programs with command line from an application的指南。

+0

kevin我剛剛更新了我的初始職位,請參閱上文。在我能夠執行腳本之前,我收到警告。謝謝 – moe 2013-04-23 16:27:33

+0

對於踢腿,嘗試將vbs腳本移動到本地驅動器,看看是否擺脫它。 – kemiller2002 2013-04-23 17:09:56