How to delete a .sdf file created in DataDirectory
I'm creating a SQL CE database file programmatically and want to make sure I'm creating a fresh new one each time so I added the delete method. Since each database file is created in DataDirectory
, I would want to delete the file in DataDirectory
as well, but it's giving me
"illegal characters in path" error
following is my code:
/* illegal characters in path */
File.Delete("|DataDirectory|\\Foo2Database.sdf");
string connString = @"Data Source=|DataDirectory|\Foo2Database.sdf";
SqlCeEngine engine = new SqlCeEngine(connString);
engine.CreateDatabase();
开发者_StackOverflow社区
|DataDirectory|
is connection string notation and is not related to file system pathes.
You can delete the file using the code like this:
var directoryName = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
var fileName = Path.Combine(directoryName, "Foo2Database.sdf");
File.Delete(fileName);
You can get current DataDirectory
via AppDomain.CurrentDomain.GetData("DataDirectory");
if you set it.
If you have asp.net DataDirectory
will be Server.MapPath("~/App_Data");
by default.
If the app is network deployed you can use this to get the path for the Data Directory
System.Deployment.Application.ApplicationDeployment.CurrentDeployment.DataDirectory
Otherwise you can use this
System.AppDomain.CurrentDomain.BaseDirectory
精彩评论