Solved

App.config fila and dynamically loading user profile

Posted on 2010-09-13
9
708 Views
Last Modified: 2013-11-27
I have an application that needs to read a file path from app.config file and the path has to contain username, based on the logged in user.

For example the path is C:\Documents and Settings\JOHNDOE\My Documents\

is there a way to do something like this:  

C:\Documents and Settings\%userprofile%\My Documents\

to make the path more dynamic?

0
Comment
Question by:YZlat
9 Comments
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 50 total points
ID: 33662393
I assume that you are logged in with that user in the application ?
.NET tip: Getting the Users Home, Temp or My Documents Directory
http://edn.embarcadero.com/article/32384
0
 
LVL 7

Expert Comment

by:klakkas
ID: 33662415
Use this to get the myDocuments folder path for the logged in user:

myDocsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
0
 
LVL 35

Author Comment

by:YZlat
ID: 33662473
Thanks, that's a good link but not exactly what I am looking for.

First of all, I need to pass the file path through app.config file in a dynamic way.

Second, the file path not necessarily will be in C:\Documents and Settings

Basically what I need to know is how to pass a varibale similar to %userprofile% through app.config file in VS.NET
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 53

Expert Comment

by:Dhaest
ID: 33662495
Inside <appSettings> Tou can create application Keys

<add key="KeyName" value="Keyvalue"/>
Later on you can access these values using:

ConfigurationManager.AppSettings["Keyname"]

See also http://stackoverflow.com/questions/603009/variables-within-app-config-web-config
0
 
LVL 17

Accepted Solution

by:
Zhaolai earned 200 total points
ID: 33662572
I do not know if it can be that dynamic, but there is a work-around:
1. Make the file path like this in the app.config:
C:\Documents and Settings\%userprofile%\My Documents\
2. After you read the file path into a string, replace the sub string "%userprofile%" with ENVIRONMENT.USERNAME

Dim strPath as String = "C:\Documents and Settings\%userprofile%\My Documents\"
strPath = strPath.Replace("%userprofile%", Environment.UserName)
 
0
 
LVL 35

Author Comment

by:YZlat
ID: 33662598
Dhaest, I know this part. The challenge is how to pass a dynamic path through kye value part, using dynamic variable similar to %userprofile%
0
 
LVL 35

Author Comment

by:YZlat
ID: 33662611
klakkas, it might not be My Documents. I used it just as an exaple
0
 
LVL 35

Author Closing Comment

by:YZlat
ID: 33662856
Thanks!
0
 
LVL 16

Expert Comment

by:13598
ID: 33662862

Still not totally clear what you are asking for. You don't seem to ask about the path so I would assume the path is not the issue. If you only want to be able to incorporate the current userprofile you could just make your app.config file contain the word 'myuserprofile' and then replace that string with the actual userprofile as in (using the path in your example):
<appSettings>
<add key="Mypath" value="C:\Documents and Settings\myuserprofile\My Documents" />
</appSettings>

To read the data back into your application, you would use something like
the following:
Dim MyPathName as String =
System.Configuration.ConfigurationSettings.AppSettings("Mypath").replace("myuserprofile", My.User.Name.ToString())
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

Suggested Solutions

Title # Comments Views Activity
Help with adding DLL file in Windows project 20 40
Problem with SelectList in .NET MVC application 1 26
VS 2017 18 29
Groupbox Control ? 2 16
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

789 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