Alex Zhukovskiy
Alex Zhukovskiy

Reputation: 10025

Get application settings without file access

I have an app.config which is working fine.

But I also have a tool for automated testing, that runs some tests in environment without any file access. So I have to read a config file from string (or memory stream), but without mapping it physically because there is no access to file system from this automatic testing process.

In real life, of course, config file is stored somewhere, but for automated testing purposes I need some workaround to read a config file from string stored in memory. Is it even possible? I googled a lot, but the only thing I found is Save it as temp file and then read, but it's not my case.

Upvotes: 0

Views: 114

Answers (3)

Scott Hannen
Scott Hannen

Reputation: 29262

Avoid a direct dependency from your class on app.config or any other file. Your class doesn't need app.config or Properties.Settings. It needs the values contained in the those files..

If you create a workaround for testing purposes then you're testing a different version of your class. That's the inherent problem - direct dependency on these files isn't testable. It doesn't mean that they're bad in some way or that we shouldn't use them, only that the class that requires the values should not read them from the file.

An ideal solution is constructor injection, a form of dependency injection. Provide the value to the class in its constructor and store it as a field. That way when the class is created it always has the values it needs.

At runtime you can use a dependency injection container - here's a walkthrough on setting one up for WCF. You're likely in a different project type, but the concepts still apply.

But for testing, it's as easy as creating a class and passing whatever value you want to use into the constructor. It's impossible to test with different values when the class reads from settings but it's easy using constructor injection.

Upvotes: 1

Yaugen Vlasau
Yaugen Vlasau

Reputation: 2218

As first option I would go for Settings file in your case. Even your user won't be ablle to access settings file content. Then it will return a default value for a particualr property.

You can try creaty a silly console app

static void Main(string[] args)
{
    Console.WriteLine(Settings.Default.MyProperty);
    Console.ReadLine();
}

were you set the your value for MyProperty on the Settings Tab of you Project Properties. Then you can build your solution, open the binaries folder and delete your exe.config file and you will see that the app will be use default values.

As second option you can use command line arguments. could also be an option for you. (Here the article about some tricky case for command line arguments Backslash and quote in command line arguments )

Third option could be placing your file at c:\users\your app user \AppData\Roaming\YourAppName folder. here you should be granted for file access even for restricted user

For education reason I would also reccomend to look at this article: https://msdn.microsoft.com/query/dev11.query?appId=Dev11IDEF1&l=EN-US&k=k(ApplicationSettingsOverview);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.5)&rd=true Maybe you find the "Load Web Settings" option nice.

Another palce for investigation could be

Upvotes: 0

Sergey L
Sergey L

Reputation: 1492

Without the configuration file you'll have the default settings. You may override the default values:

Properties.Settings.Default["PropertyName"] = NewPropertyValue";

(Set the correct access modifier on your Settings class and use the correct namespace if it is in a library)

Upvotes: 1

Related Questions