Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

CommonAppDataPath without Version Name

Posted on 2011-09-23
4
Medium Priority
?
1,663 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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
When we purchase storage, we typically are advertised storage of 500GB, 1TB, 2TB and so on. However, when you actually install it into your computer, your 500GB HDD will actually show up as 465GB. Why? It has to do with the way people and computers…
This tutorial will walk an individual through the process of installing the necessary services and then configuring a Windows Server 2012 system as an iSCSI target. To install the necessary roles, go to Server Manager, and select Add Roles and Featu…
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…

722 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