Click here to Skip to main content
16,014,765 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hey guys,

I need a little bit of help. I need to determine if a file exists in a folder in program files. If the file exists my program deletes it and then copys over the updated file to the folder.

I was using hardcoded paths when testing it and now i need some help in determining the path for 32 bit and 64 bit.

i found this forumla from googling around:

C#
static string ProgramFilesx86()
    {
        if( 8 == IntPtr.Size
            || (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
        {
            return Environment.GetEnvironmentVariable("ProgramFiles(x86)");
        }

        return Environment.GetEnvironmentVariable("ProgramFiles");
    }



I was hoping i can get some honest feedback on how reliable this is? This supposedly works for 32 bit 64 bit and 32bit running 64 bit.


And if it is reliable can you please explain how to use the formula?

I actually have a fair few files that will be copied over and if i have to use the above to determine the path everytime its going to take a long time. Is their a safe and quicker way?

Thanks.
Posted

1 solution

this[^] is what you should be using
 
Share this answer
 
Comments
burgo855 18-Jul-12 22:53pm    
Is their a way to use this in every line of code? im using this:

if (File.Exists(@"C:\PROGRA~1\test\file.txt"))
File.Delete(@"C:\PROGRA~1\test\file.txt");
File.Copy(@"C:\PROGRA~1\new\file.txt", @"C:\PROGRA~1\test\file.txt");

is their a way to use the formula in my first post in the formula above?
Christian Graus 18-Jul-12 22:55pm    
You use the method to get the base folder and then use that string to build your paths. If you've stored C:\PROGRA~1 somewhere, do a string replace to replace it with the correct string.
burgo855 18-Jul-12 23:10pm    
what string should i replace it with? Thanks.
Christian Graus 18-Jul-12 23:11pm    
The string you get back from the function I linked you to, which gets you the path to the special folder.
burgo855 18-Jul-12 23:15pm    
Cheers.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900