2017-07-14 139 views
0

即時製作一個應用程序加載器,它可以讓你有儘可能多的應用程序,你想保存在其中,例如說你想要谷歌瀏覽器,你按「添加應用程序」,你會得到一個OpenFileDialog來選擇Chrome或任何其他應用程序/你想要的程序。該程序然後將路徑和名稱保存在.bin文件中,並在單擊該按鈕時加載它。它成功地加載網站,但不應用,爲此,我認爲原因是,該方案將文件保存路徑,程序/應用程序加載器C#

C:\ Program Files文件(x86)的\谷歌\鍍鉻\應用\的chrome.exe

代替

C:/程序文件(x86)/Google/Chrome/Application/chrome.exe

至少這就是WHA我想。反正這裏是爲「保存」和「負荷」的代碼:

節省:

if (metroTextBox1.Text == "" || metroTextBox2.Text == "") 
{ 
    MessageBox.Show("You have to fill in both Name and Path first", "Invalid Info", MessageBoxButtons.OK, MessageBoxIcon.Error); 
} 
else 
{ 
    string[] name = { metroTextBox1.Text }; 
    string[] path = { metroTextBox2.Text }; 
    System.IO.File.WriteAllLines(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/appLoader/apps/appname1.bin", name); 
    System.IO.File.WriteAllLines(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/appLoader/apps/apppath1.bin", path); 
} 

負載:

try 
{      
    string path = System.IO.File.ReadAllText(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/appLoader/apps/apppath1.bin"); 
    Process.Start(path); 
} 
catch 
{ 

} 
+0

如果你認爲那是因爲做path.Replace(「/」,「\\」); – pm100

+1

查看'Path'類。 https://msdn.microsoft.com/en-us/library/system.io.path(v=vs.110).aspx –

+0

反斜槓和前斜槓,兩者都適用於'Process.Start' – Vikhram

回答

1

Process.Start()可以處理,你不必轉換任何斜線或反斜槓。像這樣啓動進程應該可以正常工作。

爲了找出錯誤,請檢查文件是否存在(File.Exists(path)),它是否可以由您直接在Windows中運行,當然(最重要的)不是像您那樣捕獲異常,而是包含被拋出的異常這樣的:

catch (Exception ex) // <-- !! 
{ 
    // investigate (and log) the exception here. 
    // note that catching all exceptions is not a good idea so narrow 
    // it down once you found the exceptions you have to care for. 
} 

也許該文件不只是不存在或無法在不having a working path set運行(這可能是強制性的一些應用)。