I store the path of my database (a folder with some xml files) in the app.config. At the startup I check, if the path exists. If it doesn't exist, I want to set the path to the default path. Code looks like this:
public void CheckAndRepairSettings()
{
/* Check Paths */
if(GetDatabasePath() == null)
SetDatabasePath(System.AppDomain.CurrentDomain.BaseDirectory + "DataBase");
}
GetDatabasePath()
reads the path form the app.config and SetDatabasePath()
writes the path to the app.config. These Methods are working fine.
My promblem is the System.AppDomain.CurrentDomain.BaseDirectory
. If I run this in my applications debug mode I get: "F:\\Office\\Projekte_Software\\ServiceTool\\_Work\\ServiceSoftware\\ServiceSoftware\\bin\\Debug\\"
I additionally use NUnit for some unit tests. If I run NUnit in debug mode I get : "F:\\Office\\Projekte_Software\\ServiceTool\\_Work\\ServiceSoftware\\ServiceSoftware.UnitTests\\bin\\Debug"
There is no trailing Backslash "\\" in the path in NUnit debug mode, so I get a non existing path when I concatenate the path-string in my CheckAndRepairSettings()
.
Why does this behave so different?
You should use Path.Combine to concatenate paths, it handles issues regarding existing/non-existing (among other things) path separators
Why one includes ending slash the other one doesn't is probably related to how nUnit creates the appdomain under which it runs its tests
A better option would be to use IsolatedStorage
!
For Example you can write settings using this:
using(IsolatedStorageFile f=IsolatedStorageFile.GetUserStoreForDomain())
{
using(var s=new IsolatedStorageFileStream("Myapp.config",FileMode.Create,f))
using(var writer=new StreamWriter(s))
writer.WriteLine("My Settings");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.