When the program starts, you must assume that the object is not aware of the source code that generated it. The code is compiled into the assembly, and this assembly is loaded when the program starts, so by default the "current" directory is the location of the executing assembly.
In short, this is not something you can do cleanly. Best of all, if your code does not rely on the concept of directories related to the source. This can help you achieve your goals if you change the build action for the file you are looking for to copy it to the output directory. Then this relative file path at run time should reflect the relative path in the project folder.
To get the path to the executing assembly (the "current" path can be changed by any other executable code, so itโs better not to rely on this), use this code:
System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
Jacob source share