Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

App.config fila and dynamically loading user profile

Posted on 2010-09-13
9
Medium Priority
?
780 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 200 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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 800 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

927 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