Solved

CommonAppDataPath without Version Name

Posted on 2011-09-23
4
1,620 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Syntax Issue with SSIS module 26 104
Amazon S3 Images with .Net 3 15
Unlocking a column in excel using C# 17 20
NTPD Client Port Usage 12 19
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this article we will learn how to backup a VMware farm using Nakivo Backup & Replication. In this tutorial we will install the software on a Windows 2012 R2 Server.
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…

828 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