scorpion35
scorpion35

Reputation: 1084

C# - Relative path of class

Trying to get relative path of config file, which is next to a class in core project, and also trying to avoid hardcoded relative paths (i.e. "../../../seleniumconfig.json")

This is my project structure,

Program.cs (in Awesome.OtherProject1)

using (var scope = Container.BeginLifetimeScope())
{
    var selenium = scope.Resolve<ZSelenium>();
    ...
}

ZSelenium.cs

var rootDir = new DirectoryInfo(Environment.CurrentDirectory);
  // "/users/myuser/Documents/MyProject/AwesomeProject.OtherProject"

I can construct my relative path from rootDir variable, but there will be a lot of hardcoded ../../.. stuff which I would like to avoid. Also, if I call ZSelenium.cs from another project, the relative path might change.

Does anyone know a better way to get the path of ZSelenium.cs within the core project?

Upvotes: 0

Views: 253

Answers (1)

Christopher
Christopher

Reputation: 9814

Configurations files do not belong into the Programm folder, ever. We did kind of do it anyway in the Windows 98 times, but it was frowned upon back then. Since XP is it not reliably possible anymore to write such a configuration file. The Programm folders is protected. Most users can only read them, not write them for security reasons. And that includes any programms started by that user too.

If you want to store user or system specific settings, that is what the special folders are there for. Their one defining purpose is that whatever user runs your programm, should have read (and likely write) access to his own and all shared folders.

You may be able to add a "default configuration" to the Programm directory to use as pattern for new users. But even for that there is a existing structure (the default user folder) so you can leave placing the template to the Setup.

Upvotes: 1

Related Questions