Opening and editing a XML config file in VB.Net

Hello,
Wondering if someone can help me here. I am needing to edit the attached XML Config file from VB.Net. The config file belongs to another program so I am not sure how to approach this.
The property that I am needing to change is the "UpdaterIntervalSeconds" property.

I did find the folowing link but cant get the code to work.
http://www.experts-exchange.com/Programming/Languages/.NET/Q_25070589.html
EmailSignatures.Client.exe.config
JNeetsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jacques Bourgeois (James Burger)PresidentCommented:
First of all, the configuration files mechanism in the framework has been designed so that a configuration file is linked with the application that uses it. They are not meant to be used or modified by other applications.

Also, note that the entry that you want to modify is in the <applicationSettings> section of the file. Application settings are designed to be read only, even for the application for which the file was designed. They are meant to be changed manually by an administrator, or through a replacement of the whole .config file.

So it appears that the decision to place that information in the configuration file is not a good one is you follow the standard rules of the framework. Data that needs to be change by many applications should normally be recorded in a database or in a custom file in the Application Data directory.

You do can modify the XML directly, but with limitations. Unless you have a custom configuration file system, the .config file is in the application directory, which is usually under Program Files, and for .NET, Program Files is read only if the user is a non administrator. You would thus need the user of the program that makes the change to be an administrator.
0
JNeetsAuthor Commented:
Just to clarify further what i am trying to achieve. the config file belongs to a program that synchronize  email signatures with a server. the property that I need to change is how often that the client checks for any changes to the email signatures on the server. as we have 50 + users editing the file on each machine is not an option. the pity is that the software does not give the user a option to change the refresh time.
0
Jacques Bourgeois (James Burger)PresidentCommented:
The easiest way to do it, in my opinion, would be to create a new file with the desired value, and use it to overwrite the original file. The administrator restriction that applies to a .NET program trying to write in Program Files does not apply to file copy, because copying files runs under Windows security and not the .NET security.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

käµfm³d 👽Commented:
They are not meant to be used or modified by other applications.
I don't know that that's necessarily true. I could see potential use cases for such. A reference for that would be nice  = )

Application settings are designed to be read only, even for the application for which the file was designed. They are meant to be changed manually by an administrator, or through a replacement of the whole .config file.
Hmmm, again I would prefer to see a reference for this. If they are meant to be read-only, then why can I modify them in code?

e.g.

Configuration cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

cfg.AppSettings.Settings["key"].Value = "blah";
cfg.Save();

Open in new window


...the .config file is in the application directory, which is usually under Program Files...
That assumes installation of the application by an installer, which even then can often times be overridden by the user.
0
Jacques Bourgeois (James Burger)PresidentCommented:
@kaufmed

I have always worked with settings using the Settings class that is generated by Visual Studio when you create your settings through the project's Properties windows (<AppName>.Properties.Settings in C#, My.Settings in VB). Through that class, application settings are ReadOnly.

So I always assumed that the application settings where designed to be modifiable only by working directly with the file. This makes sense, since Program Files is ReadOnly for most users, and most Windows applications are installed there.

The documentation for Application Settings also point to that: In most cases, the application-scoped settings are read-only. I probably missed the "in most" when I read that page more than a decade ago. If I had seen it, I would probably have looked further for an alternative.

I do not have time to test it, but do your use of the Configuration class lets a non-administrator user write to an application setting if the application is in Program Files?

Note that I wrote "usually" in Program Files. Very few users, in my experience, will install applications somewhere else than there. Unless you are deploying in-house and do not leave the control to the user, you have to assume that Program Files will be used.
0
käµfm³d 👽Commented:
I do not have time to test it, but do your use of the Configuration class lets a non-administrator user write to an application setting if the application is in Program Files?
I haven't tested that case either, but I highly doubt that it does, since Program Files is a protected directory.

Note that I wrote "usually"...
Touché.

Unless you are deploying in-house and do not leave the control to the user, you have to assume that Program Files will be used.
I agree only if you are saying that the application is installed via an installer. If you provide the user with the actual .exe, then there's no telling where the user will put it. You could certainly query for your startup path within the app itself, though.
0
Jacques Bourgeois (James Burger)PresidentCommented:
If I query for the startup path and it happens to be in Program Files, I am doomed. So I would rather assume that it will be there from the start, specially since this is the reflex for most users.

That is why I would rather stored such type of data in a custom file in the Common AppData directory instead of the .config file. AppData has been created for that.

This does not solve JNeets problem however.

@JNeets

I fall back to my original suggestion. Since you wanted to write a program to modify the XML and since it can prove to be problematic, why not write a program that simply overwrite the .config with a newer version? This should not cause problems in a standard environment, and it will also be easier to code and eventually debug for somebody who has no experience dealing with XML files through with code.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JNeetsAuthor Commented:
Just a quick update.
I anded up just opening the file in a text editior and changing the value that that way so I will split the points between JamesBurger and kaufmed Because you both provided me with valuable infomation.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

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.