Writing to Web.config with C#? 
Author Message
 Writing to Web.config with C#?

Quote:

> All was going well, until I tried to open the file. I went about creating
> the XmlDocument object, everything compiles, but when I go to run it, I get
> a parse error that dumps the following:

[snip]

> Apparently it won't let me open it with a URL, any idea why? I have tried
> the shortened name, as well as the full path. I can however get the document
> to open up by creating a TextReader object, and passing that to the Load()
> function. The properties of the TextReader will not work for what I want to
> do. Any idea what might be going wrong?

I'm having similar problems. I am trying to change the .config file of
a WinForms program written in C# from within the program. Here is what
I have learned so far:

1. After XmlDocument.Load(), you have to close the file manually,
because XmlDocument.Load() does not do that. E.g.
  using System.Xml
  XmlTextReader xmlReader = new (name);
  XmlDocument xDoc = new XmlDocument();
  xDoc.Load(xmlReader);
  xmlReader.Close();
(Not my own invention; I found this in one of the newsgroups.)

2. I use System.Reflection.Assembly.GetEntryAssembly().CodeBase to get
the name of the executable. This returns the name in URL format, i.e.
"file:///path.exe". To be able to use this in XmlTextWriter (see
below), I had to strip the "file:///" part. E.g.
  using System.Reflection;
  string prefix = "file:///";
  int prefixSize = prefix.Length;
  string oldFileName = Assembly.GetEntryAssembly().CodeBase +
".config";
  if (oldFileName.StartsWith(prefix))
    oldFileName = oldFileName.Substring(prefixSize);
Note that XmlTextReader (see above) is not bothered by the "file:///"
prefix. I don't know what happens if
System.Reflection.Assembly.GetEntryAssembly().CodeBase returns a
prefix other than "file:///", e.g. "http:///".

And now for the problem. xDoc.Save(new XmlTextWriter(oldFileName))
does not work, because access to the .config file is denied. My guess
is that the CLR is keeping it open. In the
System.Configuration.ConfigurationSettings I cannot find a way to
release to .config file. I can save the contents of xDoc to a new
file, e.g. by appending ".new" to the .config file name, but trying to
replace the .config file with the new one, using System.IO.File.Delete
and System.IO.File.Move also fail because access is denied.

Does anybody have a clue how an application can change its own
configuration settings?

Thanks in advance.

Jeroen.



Sat, 13 Mar 2004 16:13:06 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Web.config modification via C# code...

2. How to WRITE in web.config and app.config!

3. Web.config

4. web.config

5. ConfigurationSettings, get_AppSettings, Web.Config in C++

6. Assemblies element in web.config

7. Web.Config

8. Accessing Web.config file

9. cannot read web.config

10. Accessing <switches> in web.config

11. web.config - NameValueSectionHandler not found

12. web.config appSettings by User connection

 

 
Powered by phpBB® Forum Software