Solved

CommonAppDataPath without Version Name

Posted on 2011-09-23
4
1,589 Views
Last Modified: 2012-08-13
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]."
0
Comment
Question by:Dragonseer
  • 2
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
effes earned 500 total points
ID: 36589713
Try
commonAppDataPath = Path.GetDirectoryName(commonAppDataPath);

Open in new window

It returns everything up to (but not including) the last backslash.
For more info see http://msdn.microsoft.com/en-us/library/system.io.path.getdirectoryname.aspx

Hope that helps.
0
 

Author Comment

by:Dragonseer
ID: 36589741
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?
0
 
LVL 6

Expert Comment

by:effes
ID: 36589856
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.
0
 

Author Closing Comment

by:Dragonseer
ID: 36589913
Thanks!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
ConsoleSql 1 17
index Out OF Range Exception error 4 29
Setting runtime form location 4 19
Closing all open child forms. 4 23
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
I previously wrote an article addressing the use of UBCD4WIN and SARDU. All are great, but I have always been an advocate of SARDU. Recently it was suggested that I go back and take a look at Easy2Boot in comparison.
This video teaches viewers how to encrypt an external drive that requires a password to read and edit the drive. All tasks are done in Disk Utility. Plug in the external drive you wish to encrypt: Make sure all previous data on the drive has been …
This Micro Tutorial will teach you how to reformat your flash drive. Sometimes your flash drive may have issues carrying files so this will completely restore it to manufacturing settings. Make sure to backup all files before reformatting. This w…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now