-2

I have some issues with Environment.CurrentDirectory, it sometimes goes to the System32 folder. I looked online and found out why this happens and what alternatives I have (like Application.StartupPath and stuff like that) but the problem is the code is in a .dll that I am using and I can't edit it (or can I).

Is there anything I can do about this?

EDIT: In the duplicate issue the person writes their own dll. I don't own the dll I'm having problems with and I can't change it.

Angelo
  • 183
  • 1
  • 1
  • 10
  • Possible duplicate https://stackoverflow.com/questions/4764680/how-to-get-the-location-of-the-dll-currently-executing – ikkentim Sep 05 '18 at 12:19
  • You aren't quite clear what the problem is. Does the dll (i can just assume you don't have the source for that dll) changes the CurrentDirectory so your own code fails using CurrentDirectory? Or something changes CurrentDirectory and something in that dll fails? – Ralf Sep 05 '18 at 12:19
  • Possible duplicate of [How to get the location of the DLL currently executing?](https://stackoverflow.com/questions/4764680/how-to-get-the-location-of-the-dll-currently-executing) – ikkentim Sep 05 '18 at 12:20
  • 1
    If you are not sure what is causing this, then simply remember current directory on application startup and when you need to work with it either restore it or simply use the value to create absolute paths everywhere. Another thing is that your application can be executed by something else what set working directory to something you don't expect, then don't use `CurrentDirectory`, but a [more safe approach](https://stackoverflow.com/a/10993869/1997232) to retrieve directory where exe-file is located. – Sinatr Sep 05 '18 at 12:24
  • @Sinatr, @ikkentim, I cannot modify anything in the .dll as I stated, it does its own stuff and whoever made it chose (unfortunately) to use `CurrentDirectory`. @Ralf, the problem I get is that it can't find the .dll in `System32` returned by `CurrentDirectory`, which of course is only natural. – Angelo Sep 05 '18 at 13:39

1 Answers1

0

You can try to grab the path directly from the executable if CurrentDirectory is giving you an issue:

 private void GetFilePath()
        {
            string filepath = string.Empty;

            var processes = Process.GetProcessesByName("exe name");
            foreach (var process in processes)
            {
                filepath = process.MainModule.FileName;
            }

            return filepath;
        }
845614720
  • 756
  • 1
  • 7
  • 22