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)
Related
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
I am working on a C# project and I am using the following code
string rootPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
However, when I look at the rootPath its set to C:\Program Files (x86).
Why would do it this as there is an Environment.SpecialFolder.ProgramFilesX86 which I would have thought would have returned the above.
Thanks for any help you can provide
If your project is currently targeting the x86 platform, both of those enum values will return the Program Files(x86) directory.
Change the target platform for your project to x64, and SpecialFolder.ProgramFiles should return the Program Files directory instead.
I have a scenario where i could not change the target of the executing assembly. So i use this method to get always the x64 (also when running in 32bit):
var programFilesX64 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)
.OpenSubKey(#"SOFTWARE\Microsoft\Windows\CurrentVersion")?.GetValue("ProgramFilesDir");
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C# - How to get Program Files (x86) on Windows Vista 64 bit
I realize the odds of a user changing the Windows default of C:\Program Files is fairly slim, but stranger things have happened!
How can I get the correct path to Program Files from the system?
.NET provides an enumeration of 'special folders' for Program Files, My Documents, etc.
The code to convert from the enumeration to the actual path looks like this:
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
http://msdn.microsoft.com/en-us/library/14tx8hby.aspx
Just to add to this.
If you're running in 32 bit mode (even on a 64 bit os), SpecialFolder.ProgramFiles and %PROGRAMFILES% will return ..Program Files (x86).
If you specifically need one and/or the other then you'll need to check as follows:
32 bit system:
SpecialFolder.ProgramFiles = ..Program Files\
64 bit system in 32 bit process:
SpecialFolder.ProgramFiles = ..Program Files (x86)\
Environment.GetEnvironmentVariable("ProgramW6432") = ..Program Files\
64 bit system in 64 bit process:
SpecialFolder.ProgramFiles = ..Program Files\
Environment.GetEnvironmentVariable("PROGRAMFILES(X86)") = ..Program Files (x86)\
Obviously this depends on your locale etc...
You would use GetFolderPath in the Environment class.
try {
Environment.GetFolderPath( Environment.SpecialFolder.ProgramFiles )
catch( ArgumentException ex ) {
Console.Out.WriteLine( ex.StackTrace );
}
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) is probably the best solution, but another possible variant is evaluating the value of the ProgramFiles environment variable. For this, you can use the GetEnvironmentVariable or ExpandEnvironmentVariables method of the Environment class:
Environment.GetEnvironmentVariable("ProgramFiles")
Environment.ExpandEnvironmentVariables("%ProgramFiles%")
You can access the environment variable called: %PROGRAMFILES%
i.e:
%PROGRAMFILES%\Maxis\SimCity
in C#:
System.Environment.SpecialFolder.ProgramFiles
Environment.GetEnvironmentVariable("PROGRAMFILES(X86)") ?? Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
gets "program files (x86)" in 64-bits Windows and "program files" in 32 bit.
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();
}
}
I'm using:
FileInfo(
System.Environment.GetFolderPath(
System.Environment.SpecialFolder.ProgramFiles)
+ #"\MyInstalledApp"
In order to determine if a program is detected on a users machine (it's not ideal, but the program I'm looking for is a right old kludge of a MS-DOS application, and I couldn't think of another method).
On Windows XP and 32-bit versions of Windows Vista this works fine. However, on x64 Windows Vista the code returns the x64 Program Files folder, whereas the application is installed in Program Files x86. Is there a way to programatically return the path to Program Files x86 without hard wiring "C:\Program Files (x86)"?
The function below will return the x86 Program Files directory in all of these three Windows configurations:
32 bit Windows
32 bit program running on 64 bit Windows
64 bit program running on 64 bit windows
static string ProgramFilesx86()
{
if( 8 == IntPtr.Size
|| (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
{
return Environment.GetEnvironmentVariable("ProgramFiles(x86)");
}
return Environment.GetEnvironmentVariable("ProgramFiles");
}
If you're using .NET 4, there is a special folder enumeration ProgramFilesX86:
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86)
Environment.GetEnvironmentVariable("PROGRAMFILES(X86)") ?? Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
Note, however, that the ProgramFiles(x86) environment variable is only available if your application is running 64-bit.
If your application is running 32-bit, you can just use the ProgramFiles environment variable whose value will actually be "Program Files (x86)".
One way would be to look for the "ProgramFiles(x86)" environment variable:
String x86folder = Environment.GetEnvironmentVariable("ProgramFiles(x86)");
I am writing an application which can run on both x86 and x64 platform for Windows 7 and querying the below variable just pulls the right program files folder path on any platform.
Environment.GetEnvironmentVariable("PROGRAMFILES")
One-liner using the new method in .NET. Will always return x86 Program Files folder.
Environment.Is64BitOperatingSystem ? Environment.GetEnvironmentVariable("ProgramFiles(x86)") : Environment.GetEnvironmentVariable("ProgramFiles"))
C# Code:
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86)
Output:
C:\Program Files (x86)
Note:
We need to tell the compiler to not prefer a particular build platform.
Go to Visual Studio > Project Properties > Build > Uncheck "Prefer 32 bit"
Reason:
By default for most .NET Projects is "Any CPU 32-bit preferred"
When you uncheck 32 bit assembly will:
JIT to 32-bit code on 32 bit process
JIT to 32-bit code on 64 bit process