How I add Username and password and Path here?

In values i store username and password i access this how?
Vishal GundageAsked:
Who is Participating?
Dustin SaundersDirector of OperationsCommented:
If I'm understanding, your question is how do I take the keys in AppSettings and store them to then use in the second item?

If you're building an array of strings that are different values (password, username, etc.) then you don't want to iterate through them in a for loop.  So what you are doing since you have it in a for loop is running that command each time and using the array position for each setting.

For example, if you have a string array of  values = { "username", "password", "\\\\path\\" } you would need to use:
ConnectionOptions conoptions = new ConnectionOptions();
conoptions.Username = values[0];
conoptions.Password = values[1];
ManagementPath path = new ManagementPath(values[2]);

Open in new window

But even if you line up the array, that's not a good way to handle this as if your code changes you are iterating through your appsettings in no special order and the order of the array could change, breaking your code. (so don't do that)

If you have keys in your AppSettings such as "Username" and "Password", instead get those values with something like:
conoptions.Username = ConfigurationManager.AppSettings["Username"];
conoptions.Password = ConfigurationManager.AppSettings["Password"];

Open in new window

Dustin SaundersDirector of OperationsCommented:
Appropriate information to resolve this issue.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.