-1

So I created an URL protocol to run an application with the command arguments.

Here is my code:

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);
            }
        }
    }
}

The problem is that the program need some files to get launched but it can't load them because the path isn't 'set'.

How I can set this path to launch all these needed files (like /cd command)?

Unihedron
  • 10,902
  • 13
  • 62
  • 72

1 Answers1

1

If you want to set the PATH environment variable and use it in your process, add it to the Environment variables but you have to set UseShellExecute to false in that case:

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();
}
rene
  • 41,474
  • 78
  • 114
  • 152
  • 2
    I hope you realize that we don't have access to your box and you don't provide really helpful information to get an idea what you exactly need. Please read [ask] to get some guidance how to improve your question. – rene Sep 03 '14 at 11:39
  • I mean,for example,my program must have something.txt to works because the program gets some information from there,but the file can't be loaded with the url protocol handler... – Swiftiq Sep 03 '14 at 12:13
  • Are you sure you want your complete commandline INCLUDING the current application name and path and not just `Environment.GetCommandLineArgs();`? – rene Sep 03 '14 at 12:52
  • I've added some debug stuff so you can determine if there are any errors or useful output that better helps you in explaining us what is going wrong. – rene Sep 03 '14 at 13:02
  • "A local variable named 'args' cannot be declared in this scope because it would give a different meaning to 'args', which is already used in a 'parent or current' scope to denote something else" – Swiftiq Sep 03 '14 at 13:16