Determine SQL Server Compact Sdf file size programmatically

I am running an ASP.NET MVC application on shared hosting. My data is in the sdf file of the SQL Server Compact database file.

I have a limited amount of disk space. I am worried that I will go over this limit and my site will break. I can use FTP to my account and check the sdf file size, but this is a slow manual annoying process.

So, at runtime, with nothing but a connection string, can I determine the size of the database on disk?

+3


source to share


1 answer


Yes, you can.

FileInfo fi = new FileInfo(pathTosdfFile);
long fs = fi.Length; //your file size in bytes

      

btw, you can in the connection string limit the maximum size of your sdf file

Data Source=your.sdf;Max Database Size=256;

      



this will limit growth to more than 256 MB - default if I remember correctly is 128 MB

and the path to your database should look like this:

 string path =   System.IO.Path.GetDirectoryName
   (System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\your.sdf;

      

+3


source







All Articles