referencing My Documents Folder in connection string

  • Thread starter Thread starter Kerry de Vent
  • Start date Start date
K

Kerry de Vent

I am building a VB 2008 application that interfaces with a MS Access
database. I wish to change the connection string so It looks in the user's
My Documents folder rather than the current directory.

The problem is that the My Documents directory will be in a different place
depending on the user's OS and setup. Is there a way to code the connection
string so that it goes to the my documents directory wherever it may be?


connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=|DataDirectory|\studentSurvey.accdb"

thanks,
Kerry


__________ Information from ESET NOD32 Antivirus, version of virus signature database 4441 (20090919) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
dotNetDave said:
You can always replace "|DataDirectory|" with
My.Computer.FileSystem.SpecialDirectories.MyDocuments

Or straight Environment.GetFolderPath
 
When I have the connection string as

ConnectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=|My.Computer.FileSystem.SpecialDirectories.MyDocuments|\studentSurvey.accdb"

I get an error message.

Is the app.config file the correct place to put the connection string or do
I need to do it at runtime in the code?




dotNetDave said:
You can always replace "|DataDirectory|" with
My.Computer.FileSystem.SpecialDirectories.MyDocuments

David

======================================
David McCarter [Microsoft MVP]
www.dotNetTips.com
David McCarter''''s .NET Coding Standards available at:
http://codingstandards.notlong.com


Kerry de Vent said:
I am building a VB 2008 application that interfaces with a MS Access
database. I wish to change the connection string so It looks in the
user's
My Documents folder rather than the current directory.

The problem is that the My Documents directory will be in a different
place
depending on the user's OS and setup. Is there a way to code the
connection
string so that it goes to the my documents directory wherever it may be?


connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=|DataDirectory|\studentSurvey.accdb"

thanks,
Kerry


__________ Information from ESET NOD32 Antivirus, version of virus
signature database 4441 (20090919) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

__________ Information from ESET NOD32 Antivirus, version of virus
signature database 4445 (20090921) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com


__________ Information from ESET NOD32 Antivirus, version of virus signature database 4445 (20090921) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
Kerry said:
When I have the connection string as

ConnectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=|My.Computer.FileSystem.SpecialDirectories.MyDocuments|\studentSurvey.accdb"

You need to build the string dynamically, with String.Format for
example. The My.Computer.FileSystem.SpecialDirectories.MyDocuments
property is returning a string in the application. You put it's results
in the connection string.
 
Back
Top