[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Opening and editing a XML config file in VB.Net

Posted on 2014-08-24
8
Medium Priority
?
542 Views
Last Modified: 2014-08-26
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
0
Comment
Question by:JNeets
  • 4
  • 2
  • 2
8 Comments
 
LVL 40
ID: 40282723
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
 

Author Comment

by:JNeets
ID: 40282757
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
 
LVL 40
ID: 40283136
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 750 total points
ID: 40283224
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
 
LVL 40
ID: 40283361
@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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40283382
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
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 750 total points
ID: 40283517
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
 

Author Closing Comment

by:JNeets
ID: 40287186
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

831 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