2010-08-11 48 views
7

我一直在嘗試從C#應用程序啓動應用程序,但無法正常啓動。在cmd中,應用程序和參數啓動一個顯示輸出的小窗口,然後將應用程序最小化到系統托盤中。C#使用多個參數啓動應用程序

使用下面的代碼從C#應用程序啓動應用程序會導致進程出現在任務管理器中,但沒有其他任何東西,沒有輸出窗口,也沒有系統托盤圖標。可能是什麼問題?

myProcess.StartInfo.FileName = ...; 
    myProcess.StartInfo.Arguments = ...; 
    myProcess.Start(); 

也嘗試過使用

Process.Start(Filename, args) 

也沒有工作通過以下

myProcess.StartInfo.RedirectStandardOutput = true; //tried both 
    myProcess.StartInfo.UseShellExecute = false; //tried both 
    myProcess.StartInfo.CreateNoWindow = false; 

。真的很感謝有關如何解決這個問題的任何幫助。

更新: 我認爲這個問題也許多個參數將被傳遞到過程

RunMode=Server;CompanyDataBase=dbname;UserName=user;PassWord=passwd;DbUserName=dbu;Server=localhost;LanguageCode=9 

問候

+0

嘗試將參數括在單引號中。 – leppie 2010-08-11 08:22:21

+0

@leppie無變化 – artsim 2010-08-11 08:38:15

回答

1
 System.Diagnostics.Process.Start(FileName,args); 

 System.Diagnostics.Process.Start("iexplore.exe",Application.StartupPath+ "\\Test.xml"); 
+0

我曾試過,但它仍然無法正常工作 – artsim 2010-08-11 08:39:32

7

我不查看代碼中的任何錯誤。我寫了一個小程序,打印出它的參數傳遞給控制檯

static void Main (string[] args) 
{ 
    foreach (string s in args) 
     Console.WriteLine(s); 
    Console.Read(); // Just to see the output 
} 

,然後我已經把它放在C:作爲應用「PrintingArgs.exe」的名字,所以我寫了另外一個,執行第一個:

Process p = new Process(); 
p.StartInfo.FileName = "C:\\PrintingArgs.exe"; 
p.StartInfo.Arguments = "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18"; 
p.Start(); 

這給出了數字列表的所需輸出。調用PrintingArgs的應用程序在到達p.Start()時退出,這可以通過使用p.WaitForExit();或僅使用Console.Read();來避免。 此外我還使用p.UseShellExecutep.CreateNoWindow。只有在

p.UseShellExecute = false; 
p.CreateNoWindow = true; 

使得PrintingArgs應用程序不顯示窗口(即使我只放p.CreateNoWindow = true它顯示了一個窗口)的情況。

現在我想到,也許你的錯誤傳遞參數,並使其他程序失敗並立即關閉,或者你沒有指向正確的文件。檢查路徑和名稱,以查找可能遺漏的任何錯誤。 此外,使用

Process.Start(fileName, args); 

不使用你設置了的StartInfo的信息到你的流程實例。

希望這將幫助, 問候

2

具有u設置你的ProcessWindowStyle爲隱藏? 這是我的代碼,做工精細:

Process p=new Process(); 
p.StartInfo.FileName = filePath;//filePath of the application 
p.StartInfo.Arguments = launchArguments; 
p.StartInfo.WindowStyle = (ProcessWindowStyle)ProcessStyle;//Set it to **Normal** 
p.Start(); 
4

不知道是否有人依然遵循着這一點,但這裏是我想出了。

string genArgs = arg1 + " " + arg2; 
string pathToFile = "Your\Path"; 
Process runProg = new Process(); 
try 
{ 
    runProg.StartInfo.FileName = pathToFile; 
    runProg.StartInfo.Arguments = genArgs; 
    runProg.StartInfo.CreateNoWindow = true; 
    runProg.Start(); 
} 
catch (Exception ex) 
{ 
    MessageBox.Show("Could not start program " + ex); 
} 

在字符串中添加空格允許將兩個參數傳遞到我想運行的程序中。執行代碼後程序運行沒有問題。