2014-09-02 39 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