I was using hard-coded directory path to Program Files to move file. I would now like to use the correct method to find the folder in Program Files.
I have found this method doing some Googling and it is what i would like to use:
static string ProgramFilesx86()
{
if( 8 == IntPtr.Size || (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
{
return Environment.GetEnvironmentVariable("ProgramFiles(x86)");
}
return Environment.GetEnvironmentVariable("ProgramFiles");
}
I unfortunately am not sure how to implement and use this method.
- Where do i insert the method in my app?
-
How do i use the above instead of this:
if (File.Exists(@"C:\PROGRA~1\TEST\ok.txt")) File.Delete(@"C:\PROGRA~1\TEST\ok.txt"); File.Copy(@"C:\PROGRA~1\PROGRAMFOLDER\ok.txt", @"C:\PROGRA~1\TEST\ok.txt");
It’s much easier to get the special folders like Program Files using
Environment.SpecialFolders
Continuing that example you could do something like this
UPDATE
Modified the code example to always get the 32-bit Program Files folder whether you’re running 32- or 64-bit OS as @Mario pointed out that’s what your original code was doing.