0

I am new to C# and I have made a simple Windows Forms Application that basically updates the persons files for a game.

They have to manually move and delete certain folders just to change version every time. I have successfully accomplished this.

However before I start giving it out I really should improve it. I know I need to change the name of the processes and remove my descriptions ETC.

I have stumbled onto an error and instead of me taking a guess I think it is best to get an opinion from a more experienced person about how to do this.

I am going to use Inno Setup to make the installer for my application, this way I can be sure it will go into their program files 32 and 64 bit. So I know this will be in program files.

So now I am wondering if I have done this the correct way or not? I was using this format to find their program files:

string programFilesFolder = Environment.GetEnvironmentVariable("PROGRAMFILES(X86)") ?? Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);

However, would this work on all windows systems(XP, Vista, Win7, Win8) and is it completely accurate? I was going to use the above, and then use this:

        string PATCHSELECTOR = Path.Combine(programFiles, @"PATCH SELECTOR");
        if (Directory.Exists(PATCHSELECTOR))
        {
            string GamereliteFolder = Path.Combine(programFiles, @"GAMERELITE~1");
            if (Directory.Exists(GamereliteFolder))

And then I move the files using the string method. If the file exists it is deleted before I copy the file over from PATCH SELECTOR to GAMERELITE.

Also will windows XP support using the .exe with an assembly resource embedded which is making the program need to be ran as administrator? I previously was making the assembly work through UAC however that wouldnt always work if they have UAC off or if it is XP so I thought I would try the admin assembly instead.

Can anyone possibly give me some insight, ideas or links?

Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
Burgo855
  • 248
  • 1
  • 5
  • 19

3 Answers3

0

This method works for any executable located in a folder which is defined in the windows PATH variable:

private string LocateEXE(String fileName)
{
    string path = Environment.GetEnvironmentVariable("path");
    string[] folders = path.Split(';');

    foreach (var folder in folders)
    {
        if (File.Exists(Path.Combine(folder, fileName))) 
        {
            return Path.Combine(folder, fileName);
        }
    }

    return String.Empty;
}

Usage:

string pathToEXE = LocateEXE("Example.exe");

Reference:

Community
  • 1
  • 1
Furqan Safdar
  • 16,260
  • 13
  • 59
  • 93
0

For executables (not sure for websites & web application) this returns the directory where the executable lives (it's actually the base path where the framework will probe for Assemblies to load, 99% of the the that's the same thing).

System.AppDomain.CurrentDomain.BaseDirectory
Binary Worrier
  • 50,774
  • 20
  • 136
  • 184
0

Couple things:

  • Among the already stated answers, Assembly.GetExecutingAssembly().Location will also give you the full file path of the currently "executing" Assembly. (Alternatively, GetCurrentAssembly)

  • If I'm reading your question correctly, you're trying to find both your own location as well as another application's. I would highly recommend seeing if the other application has a registry key that specifies the exact location - it'll make your copy step WAY more stable.

JerKimball
  • 16,584
  • 3
  • 43
  • 55