Solved

App.config fila and dynamically loading user profile

Posted on 2010-09-13
9
663 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now