mercredi 8 avril 2015

Get data source file extension from SqlConnection

Is there a way to retrieve datasource file extension from a SQLiteConnection instance, without having to depend on applying regular expressions or similar string management on the connection string?


Maybe using another library apart from System.Data.Sqlite?


If there were no other way to get file extension than by means of regex, which regular expression would you reccomend? I am concerned about "weird" file/fodler names, such as placing the datasource file inside a folder such as * Data Source= =testDB.db;Version=3; * (all these are valid characters)





  • Code example


In this example, I create a SQlite connection from an input connection string located in an app.config file. Later, I open the connection and I am able to get current directory and datasource file name, but no extension.



var connString = @"data source=|dataDirectory|\testDB.db;Version=3;";
string FullDataSourceName = null;

using (var conn=new System.Data.SQLite.SQLiteConnection(connString,true))
{
conn.Open();
string currentExecutablePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string DataSourcePath = (System.IO.Path.GetDirectoryName(currentExecutablePath));
string DataSourceName = conn.DataSource;

//I can get datasource file name and path, but no extension
FullDataSourceName = Path.Combine(DataSourcePath, DataSourceName);

}

//this miserably fails if no extension is added to the string
System.IO.File.Delete(FullDataSourceName);




Regards


Aucun commentaire:

Enregistrer un commentaire