views:

143

answers:

4

I am trying to start a program I made in this directory:

C:\example\example.exe -someargument

when the computer starts up. I am attempting to use this registry key:

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run

with the key being:

Name: example
Type: REG_SZ
Data: "C:\example\example.exe -someargument"

But my program also needs files from the directory C:\example but can't find them since the current working directory is different. Is is possible to do something like this in the registry key value

"cd C:\example\; example.exe -someargument"

so that it will change the directory? Or is there a better solution?

Thanks!

A: 

If the files are always going to be in the same directory as your application, use the Application.ExecutablePath to locate the working directory for the files from within your code, then you can reference them no matter what.

Tom
+2  A: 

You can register your application under next registry key (like this does Reg2Run tool)

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\App Paths\example.exe

@="c:\example\example.exe"
Path="c:\AnotherPath"

So System.Diagnostics.Run("example.exe"); will launch your application with specified working path.

Or another way: write a launcher using C#. You can do the same using a PowerShell cmdlet.

var info = new System.Diagnostics.ProcessStartInfo(@"c:\example\example.exe", "-someargument")
{
    WorkingDirectory = @"c:\AnotherPath"
};
System.Diagnostics.Process.Start(info);
abatishchev
A: 

If you need load DLLs from the same directory you can create subkey example.exe under

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths

registry key and define PATH REG_SZ value example.exe

Oleg
A: 

At the start of the application, do the following (this is C#, convert to C++):

    using System.IO;
:
:
    Environment.CurrentDirectory = Path.GetDirectoryName(Application.ExecutablePath);
Simon Chadwick
This is just a code snippet giving guidance. The "using" statement is just there in case your app doesn't already have it. Yes, go ahead and fully qualify all the other class names shown if you need to, depending on your code, but please don't down-vote a perfectly good solution by being so pedantic. That is not the SO spirit.
Simon Chadwick