Solved

App.config fila and dynamically loading user profile

Posted on 2010-09-13
9
694 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

DevOps Toolchain Recommendations

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

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Latency in .net app using DB in .net 21 60
Variable Event ? 3 27
How to join on ID, with prefix? 15 58
Import a txt file into a DataGridView and TextBox 20 33
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

815 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

8 Experts available now in Live!

Get 1:1 Help Now