Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

CommonAppDataPath without Version Name

Posted on 2011-09-23
4
Medium Priority
?
1,679 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 2000 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Windows Server 2003 introduced persistent Volume Shadow Copies and made 2003 a must-do upgrade.  Since then, it's been a must-implement feature for all servers doing any kind of file sharing.
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…
Suggested Courses

885 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