How to remove .sdf file created in DataDirectory

I am creating a SQL CE database file programmatically and want to create a new new one each time, so I added a delete method. Since each database file is created in DataDirectory, I would also like to delete the file in DataDirectory, but it gave me

"illegal characters in transit" error

the 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();
+3
source share
2 answers

|DataDirectory| - This is the designation of the connection string and is not related to the file system.

You can delete the file using the following code:

 var directoryName = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
 var fileName = Path.Combine(directoryName, "Foo2Database.sdf");
 File.Delete(fileName);

You can get the current DataDirectoryone through AppDomain.CurrentDomain.GetData("DataDirectory");if you install it.

If you have asp.net DataDirectory, the default will be Server.MapPath("~/App_Data");.

+4
source

,

System.Deployment.Application.ApplicationDeployment.CurrentDeployment.DataDirectory

System.AppDomain.CurrentDomain.BaseDirectory
+1

All Articles