Launch application from its directory [duplicate] - c#

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!

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

At the start of the application, do the following (this is C#, convert to C++):
using System.IO;
:
:
Environment.CurrentDirectory = Path.GetDirectoryName(Application.ExecutablePath);

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

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.

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

Related

My Program doesn't recognize other files when it starts from the registry [duplicate]

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!
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);
At the start of the application, do the following (this is C#, convert to C++):
using System.IO;
:
:
Environment.CurrentDirectory = Path.GetDirectoryName(Application.ExecutablePath);
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
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.
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

Associated extension doesn't send file name to application on double click

I have specified a file extension to be associated with my program (Window Application) through Project Properties >> Publish >> Options >> File Associations in Visual Studio 2013.
I know that if I drag one or several files and drop it on my application (.exe), I can catch all the paths through the arguments (string[] args) of my Main method (located in Program.cs). But when I open an associated file (which launches my published and installed application), the path of the file is not passed as an argument to my Main method.
How can I catch the path of the file(s) which has launched my application?
BTW, I can also use registry (HKEY_CLASSES_ROOT) to associate file extensions with my application beside using Visual Studio's "File Associations" feature. Which method do you recommend the most and why?
Check the registry editor:
[Win+R], type "regedit"
Open HKEY_CLASSES_ROOT
Open the .yourfileextension key
In the key where your path of the executable is stored check if there's an %%1
if not rightclick and Edit it like this: "C:\Path\to\Executable\executable.exe" %%1
I actually solved my own problem with the following code:
RegistryKey command, defaultIcon, extension;
// Create Keys
command = Registry.CurrentUser.CreateSubKey(#"Software\Classes\APP NAME\shell\open\command");
defaultIcon = Registry.CurrentUser.CreateSubKey(#"Software\Classes\APP NAME\DefaultIcon");
extension = Registry.CurrentUser.CreateSubKey(#"Software\Classes\.EXTENSION");
// Create Values
command.SetValue("", "\"" + Application.ExecutablePath + "\" %1", RegistryValueKind.String);
defaultIcon.SetValue("", "ICON PATH", RegistryValueKind.String);
extension.SetValue("", "APP NAME", RegistryValueKind.String);
APP NAME is where I put the name of my application,
EXTENSION is where I put the extension I want to associate with my application, and ICON PATH is the path to the icon file which I want the associated files to have.
The %1 at the end of the ExecutablePath makes the path of the double-clicked associated file to be passed as an argument to Main method of my application.

Path of %ProgramFiles(x86)% in 64 bit machine (for Registry)

Question: What is the equivalent of [INSTALLDIR] for %ProgramFiles(x86)% to use in Registry in 64 bit machine ?
I have a program that will be installed inside %ProgramFiles(x86)% in 64 bit machine.
Basically, I want to add these values in registry
Value name:
(Default)
Value data:
"C:\Program Files (x86)\MyApp\MyApp.exe" "%1"
The above Value data works just fine but I cannot use the exact path because the Windows might be installed in a different directory other than C:\
I tried
Value data:
"[INSTALLDIR]MyApp.exe" "%1"
but it gives application not found error.
What can I use to get the path of %ProgramFiles(x86)% in registry?
Any help will be really appreciated.
If your installer is marked x64, you can use the ProgramFilesFolder installer property:
"[ProgramFilesFolder]MyApp\MyApp.exe" "%1"
In x64 mode, this property will point to the x86 Program Files folder, and ProgramFiles64Folder will point to the x64 Program Files folder.
EDIT: If you import a reg file into the registry instead of having the installer generate the keys and values, you can use an environment variable instead:
"%ProgramFiles(x86)%\MyApp\MyApp.exe" "%1"
Possibly duplicate here.
static string ProgramFilesx86()
{
if( 8 == IntPtr.Size
|| (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
{
return Environment.GetEnvironmentVariable("ProgramFiles(x86)");
}
return Environment.GetEnvironmentVariable("ProgramFiles");
}
[INSTALLDIR] includes the name of your application. So it translates to
C:\Program Files (x86)\MyApp\MyApp\MyApp.exe in your example. Try using
"[INSTALLDIR]MyApp.exe" "%1"
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86)

|DataDirectory| in Project properties > Settings

The connection string setting is below:
Name:
dbPersonConnectionString
Type:
Connection string
Scope:
Application
Value:
Data Source=|DataDirectory|\dbPerson.sdf
When I install & run the application, it looks for DB in C:\MyApp\Data\ folder. It should be C:\MyApp without additional \Data folder.
Should I simply create Data folder in my project and move DB files under that folder or I simply adjust |DataDirectory| -and how-?
EDIT:
string executable = System.Reflection.Assembly.GetExecutingAssembly().Location;
string path = (System.IO.Path.GetDirectoryName(executable));
AppDomain.CurrentDomain.SetData("DataDirectory",path);
This has been asked before. This MSDN post gives a good overview.
It should indeed default to your binaries folder, you can change it with AppDomain.SetData() . If you change it, better do it early.
AppDomain.CurrentDomain.SetData("DataDirectory", Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
This should work always because Directory.GetCurrentDirectory() may return other directory than the executable one
This one solved my problem
AppDomain.CurrentDomain.SetData("DataDirectory", Directory.GetCurrentDirectory());

How to get installation path of an application?

In Windows using C#, how can I get the installation path of a software (for example consider NUnit or any other software like MS word, etc.) from my project? Also how to set the path variables that we set in Environment variables so that we can run the application just by giving in command prompt.
Like if I install NUnit in "C:\Program Files" I can run it by giving 'NUnit' in cmd prompt but if I install in a different location I can't do the same.
I need to get the location or path of NUnit or any other software installed in my system (having Windows XP) from my project.
EDIT:
Like I can get the path of installed program from registry.
HKEY_CURRENT_USER->SOFTWARE
Use the system and application classes. This will give you all sorts of information.
EG: Application.ExecutablePath
It also provides methods to do what you want to.
Edit: Also see registry read/write instructions here:
http://www.c-sharpcorner.com/UploadFile/sushmita_kumari/RegistryKeys102082006061720AM/RegistryKeys1.aspx?ArticleID=0ce07333-c9ab-4a6a-bc5d-44ea2523e232
Application.ExecutablePath (includes filename)
Application.StartupPath (not includes filename)
This will give you the path where the application started. Hopefully it will be the installation path.
string appFileName = Environment.GetCommandLineArgs()[0];
will give you the full path of the executable and
string directory = Path.GetDirectoryName(appFileName);
extracts the directory.
string envPath = Environment.GetEnvironmentVariable("PATH");
Environment.SetEnvironmentVariable(envPath + ";" + yourPath);
edits the PATH environment variable for the current process.
Application.StartupPath is used to get installation location in c#.
Like if i install Nunit in "C:\Program
Files" i can run it by giving 'nunit'
in cmd prompt but if i install in a
different location i cant do the same.
May be you are using Windows Vista, which can search in Program Files, but won't look in other folders.
In windows using C#, how to get the
installation path of a software(for
example consider nunit).?
It depends, how you are installing the application. The installer knows the path, you may program the installer to write that path to somewhere, say registry.
Also how to set the path variables
that we set in Environment variables
so that we can run the application
just by giving in command prompt.
How do I get and set Environment variables in C#?
Steps to extract value from registry are shown in following code snippet.
You may already know that there are no standard rules for applications to place their installation info.
The steps shown below are for COM based applications where the appplication must provide Local executable path in a reasonably standard manner.
For non-com applications, check to see if some data can be extracted from installed applications cache.
I hate to admit that the solution is not as elegant as I want it to be. Each subkey has to opened in series and opening in single method does not work.
//string hiveName = #"CLSID"; // for 64 bit COM 7applications
string hiveName = #"WOW6432Node\CLSID"; // for 32 bit COM applications
using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(hiveName))
{
if (key != null) {
using (RegistryKey key2 = key.OpenSubKey("{<YourAppGUID>}"))
{
if (key2 != null) {
using (RegistryKey key3 = key2.OpenSubKey("LocalServer32"))
{
if (key3 != null) {
return key3.GetValue("").ToString();
}
}

Categories