2014-09-02 40 views
-1

因此,我創建了一個URL協議以使用命令參數運行應用程序。在c#中爲URL協議設置路徑#

這裏是我的代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.IO; 
using Microsoft.Win32; 
using System.Diagnostics; 

namespace iw4Protocol 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      RegistryKey key = Registry.ClassesRoot.OpenSubKey("gameProtocol"); 

      if (key == null) 
      { 
       string iw4FullPath = Directory.GetCurrentDirectory(); 

       gameProtocol protocol = new gameProtocol(); 
       protocol.RegisterProtocol(gameFullPath); 
      } 
      else 
      { 
       RegistryKey gamepathkey = Registry.ClassesRoot.OpenSubKey("gameProtocol"); 
       string gamepath = gamepathkey.GetValue("gamepath").ToString(); 

       Environment.SetEnvironmentVariable("path",gamepath); 



       ProcessStartInfo startInfo = new ProcessStartInfo(); 
       startInfo.FileName = @"test.exe"; 
       startInfo.Arguments = Environment.CommandLine; 
       Process.Start(startInfo); 
      } 
     } 
    } 
} 

的問題是,該方案需要一些文件即可啓動,但由於路徑沒有「設置」無法加載它們。

如何設置此路徑來啓動所有這些所需的文件(如/cd命令)?

+0

是Environment.SetEnvironmentVariable(」路徑」,gamepath);排隊那個拋出錯誤的那個?如果是這樣,是不是PATH? – 2014-09-02 15:39:24

+0

http://msdn.microsoft.com/en-us/library/cb20e19t.aspx – Donal 2014-09-02 15:41:34

+0

@DanielCasserly試了一下,並改變了一切。 – Swiftiq 2014-09-02 15:50:21

回答

1

如果要設置PATH環境變量,並用它在你的過程中,將其添加到Environment variables但你必須設置UseShellExecute爲false在這種情況下:

ProcessStartInfo startInfo = new ProcessStartInfo(); 
// set environment 
startInfo.UseShellExecute = false; 
startInfo.EnvironmentVariables["PATH"] += ";" + gamepath; 
// you might need more Environment vars, you're on your own here... 
// start the exe 
startInfo.FileName = @"test.exe"; 
startInfo.Arguments = Environment.CommandLine; 

// added for debugging 

startInfo.RedirectStandardOutput = true; 
startInfo.RedirectStandardError = true; 


var p = new Process(); 
p.StartInfo = startInfo; 

using(var sw = new StreamWriter(File.Create("c:\\temp\\debug.txt"))) // make sure C:\temp exist 
{ 
    p.OutputDataReceived += (sender, pargs) => sw.WriteLine(pargs.Data); 
    p.ErrorDataReceived += (sender, pargs) => sw.WriteLine(pargs.Data); 

    p.Start(); 
    p.BeginOutputReadLine(); 
    p.BeginErrorReadLine(); 

    p.WaitForExit(); 
} 
+0

現在,它啓動該程序;但仍然沒有所需的文件... – Swiftiq 2014-09-03 11:36:16

+2

我希望你意識到我們沒有訪問你的盒子,你不提供真正有用的信息,以瞭解你確切需要什麼。請閱讀[問]獲得一些指導如何改善你的問題。 – rene 2014-09-03 11:39:24

+0

我的意思是,例如,我的程序必須具有something.txt才能工作,因爲程序從那裏獲取了一些信息,但該文件無法使用url協議處理程序加載... – Swiftiq 2014-09-03 12:13:17