Link to home
Start Free TrialLog in
Avatar of Dragonseer
DragonseerFlag for United States of America

asked on

CommonAppDataPath without Version Name

Hello All.

I have a program which has a SQL CE Database file which is the core database that the program relies on which I am storing in CommonAppDataPath. However I noticed that CommonAppDataPath returns this format: "C:\\ProgramData\\Company Name\\Program Name\\1.0.0.0"

The database file will stay consistent regardless of the version of the program so I would like to store the file in "C:\\ProgramData\\Company Name\\Program Name\\".

Currently, I am using the below code to remove the last 8 characters of the string which are "\\1.0.0.0" and it appears to be working.
 
string commonAppDataPath = Application.CommonAppDataPath.ToString();

                for (int i = 0; i < 8; i++)
                    commonAppDataPath = commonAppDataPath.Remove(commonAppDataPath.Length - 1);

Open in new window


However, I am wondering if there is an "official" way to do this as I am concerned that this method may break since CommonAppDataPath can be unique on every machine.

Am I doing this the best way possible or is there a better method that I can use?

Edit: I also seem to get the following message when the SQL CE file is in the version folder. The program works perfectly fine if I concanate it myself and write to the folder above the version directory, as above. "Access to the database file is not allowed. [ 1884, File Name = C:\\ProgramData\\Company Name\\Program Name\\1.0.0.0\\database.sdf, SeCreateFile]."
ASKER CERTIFIED SOLUTION
Avatar of effes
effes
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dragonseer

ASKER

Hello effes.

Your solution does appear to work. Thanks.
However, my second question remains: how safe is this and is it likely to break due to the possibility of the CommonAppDataPath being unique on each machine?
Well, this method works with every path you throw at it (as long as its not the root directory). It is an official part of the .NET Framework and it is designed to do what you need.
So yes, I think it is pretty save and unlikely to break because of a different path.

As an alternative you could use the DirectoryInfo class to get the parent folder of CommonAppDataPath:
DirectoryInfo di = new DirectoryInfo(commonAppDataPath);
commonAppDataPath = di.Parent.FullName;

Open in new window

But that seems to be overkill.
Thanks!