Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

App.config fila and dynamically loading user profile

Posted on 2010-09-13
9
Medium Priority
?
763 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

688 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