What is a connection string to sdf in Common Files Folder?

Posted on 2014-10-05
Last Modified: 2014-10-05
Hello Experts

My Desktop Windows (X86) Application failed on Vista and XP machines because I had an sdf in the Program Files and/or Program Files (x86) folder that was accessed using the connection string:

Dim getSW As New SqlCeConnection("Data Source =" & Application.StartupPath & "\Volume Tables\Softwood\" & lblSW.Text & ".sdf")

That was throwing an error because permissions do not exist.

Since these files will be shared, I assume that I should drop the folder and files into the Common Files Folder for it to work: Feel free to correct me if I am wrong. I have done that using the File System component and it works as far as install goes. What I do not know is how to write a connection string to the location. I tried:

Dim getSW As New SqlCeConnection("Data Source =" & Application.CommonAppDataPath & "\Volume Tables\Softwood\" & lblSW.Text & ".sdf")
It does not work. I do not know how to connect to the files in the Common Files Folder. And even if I can connect will users still be locked out from reading sdf files in that location? There is no write access required.

How do I fix this? Using Compact Server 3.5 SP2.

Question by:PBLack
  • 3
  • 3

Author Comment

ID: 40362458
I couldn't figure it out so I went with using User's Personal Folder in my compile and then referenced it in my app with System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

It works unless someone has a better option.
LVL 40
ID: 40362478
Under .NET, Program Files is read-only for users who are not administrators, so you have to move files that will be written to somewhere else.

But CommonAppDataPath is not Common Files. It's not common for all users, it is common for all applications. If you look at what it gives, it is something like C:\ProgramData\JamesBurger\JBBackup\, Where JamesBurger is the user name, JBBackup is the Application name and then the application version number.

What I do when I need to share files between users is to store them somewhere under C:\Users\Public. Because you are working with different operating systems however, Users can have a different name, such as Documents and Settings. In French it's Utilisateurs. For that reason, you are better to retrieve the name from the OS with the following command: Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
LVL 40
ID: 40362494
I was writing my answer while you posted your solution. A bad one.

First of all, you won't be able to share the files. MyDocuments defaults to the current user.

And worse than that, you are doing a mistake that is too common: My Documents should be used for my documents, not yours. It should be reserved for documents that the user create and Save or Save As. The user does not know what a .mdf or a .sdf is, so it does not belong there. On your side, you risk that the user will delete the database because he's cleaning up and think that these files are useless.

Unfortunately, too many programmers and companies use MyDocuments as an easy way out around the extra security that permeates the systems nowadays. MyDocument as become a mess for most users, with they work mixed up with a lot of stuff that does not belong there.

Do you see Outlook files in My Documents? Do you see the custom dictionaries created in Office? Although the user creates the information in there, he knows nothing about the necessary files and how to manage them. So Microsoft saves then under Program Data, your first idea (Application.CommonAppDataPath). The convention that files created by and for an application, but not directly by the user should be in CommonAppDataPath\CompanyName\ApplicationName.

This is good for files such a the ones mentioned. But in your case, you need to share the files, so a directory that is in the users own personal folder won't cut it. That is why I suggest to store it in the Public folder, because it is shared by everybody without having to change the security on the computer.
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.


Author Comment

ID: 40362616
Thank you for the advice about My Documents. I will not be using it. Also, having just spoke with the end user, the files will NOT be shared. In fact he wants it so users can operate independently of each other. I had said only read access. Now he requires Read and Write. Sorry for the confusion.,

So what do I use to put the sdfs in a folder that is unique to the user but still allow read and write access?

LVL 40

Accepted Solution

Jacques Bourgeois (James Burger) earned 500 total points
ID: 40362629
environment.GetFolderPath(environment.SpecialFolder.LocalApplicationData), which will gives you something like "C:\Users\JamesBurger\AppData\Local" (on Windows 7 and more, somewhere in Documents and Settings for XP if my memory is good).

As told before, the convention is to create a subdirectory with the CompanyName\ApplicationName and put the application data there.

Author Comment

ID: 40362666
Thank you my good man. You have been a tremendous help. Guys like you are the reason I have been a member here for so many years!

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

786 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