Joel
Joel

Reputation: 15526

Use registry to startup a program, and also change the current working directory?

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!

Upvotes: 11

Views: 15925

Answers (5)

Yuri
Yuri

Reputation: 31

You can also create a shortcut for the program in the folder and reference this shortcut in the registry:

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
Name: example
Type: REG_SZ
Data: "C:\example\example.lnk

Upvotes: 3

abatishchev
abatishchev

Reputation: 100368

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

Upvotes: 13

Simon Chadwick
Simon Chadwick

Reputation: 1148

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

    using System.IO;
:
:
    Environment.CurrentDirectory = Path.GetDirectoryName(Application.ExecutablePath);

Upvotes: 6

Oleg
Oleg

Reputation: 222017

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

Upvotes: 0

Tom A
Tom A

Reputation: 1682

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.

Upvotes: 0

Related Questions