How to use an XML config file in Windows Service

  • Thread starter Thread starter SP
  • Start date Start date
S

SP

Please advice. I am not familiar with the concepts of windows service.

I am trying to access a xml configuration file from windows service. But it
is failing. configuration file is in the same directory from where I install
windows service. I dont want to give an absolute path to the location of
file. When I try to start windows service, it is expecting the XML file to
be in c:\windows\system32\.

Please let me know whether there is a way to access xml files from windows
service.

Thanks,
SP
 
You can use the System.Configuration.ConfigurationSettings.AppSettings
to store the settings in a file.

Lowell
 
Lowell,

I have to store hierarchical data which app.config does not support. So I am
using a .xml file for that job. I cant use your suggested method.

Thanks,
SP
 
SP said:
Lowell,

I have to store hierarchical data which app.config does not support. So I am
using a .xml file for that job. I cant use your suggested method.

Thanks,
SP

This might be a bit "round-a-bout", but you could store the path to YOUR
config file in the app.config file.

Chris
 
SP said:
Please advice. I am not familiar with the concepts of windows service.

I am trying to access a xml configuration file from windows service. But
it
is failing. configuration file is in the same directory from where I
install
windows service. I dont want to give an absolute path to the location of
file. When I try to start windows service, it is expecting the XML file to
be in c:\windows\system32\.

Please let me know whether there is a way to access xml files from windows
service.

The Windows service starts with a default directory of C:\Windows\System32.
You simply need to specify the full path to the XML file. You can get the
executables directory from:

System.AppDomain.CurrentDomain.BaseDirectory
 
Use XmlSerializer to serialize/deserialize class from disk or other. You
can include code like below for your class. Your class should contain
public fields/properties for the fields you want serialized.

// In the declaration section. My class here is XmlReply, yours is what
ever you want.
private static XmlSerializer ser = new XmlSerializer(typeof(XmlReply));

// Methods in your class

public string ToXmlString()
{
byte[] bytes = ToBytes(false);
return Encoding.UTF8.GetString(bytes);
}

public static XmlReply FromXmlString(string xmlString)
{
if ( xmlString == null )
throw new ArgumentNullException("xmlString");

XmlReply xr = null;
using (StringReader sr = new StringReader(xmlString))
{
xr = (XmlReply)ser.Deserialize(sr);
return xr;
}
}

/// <summary>
/// Serialize class to utf-8 encoded byte array. Serialize defaults to
/// using UTF8 so we can avoid an additional XmlWriter stream.
/// </summary>
/// <param name="prefixLen"></param>
/// <returns></returns>
public byte[] ToBytes(bool prefixLen)
{
using(MemoryStream ms = new MemoryStream())
{
if ( prefixLen )
ms.Position = 4;

ser.Serialize(ms, this, NS);

if ( prefixLen )
{
if ( ms.Length > uint.MaxValue )
throw new ArgumentException("Serialized class bigger then
uint.MaxValue");
uint len = (uint)ms.Length;
byte[] lenBytes = BitConverter.GetBytes(len);
ms.Position = 0;
ms.Write(lenBytes, 0, lenBytes.Length);
}
return ms.ToArray();
}
}

/// <summary>
/// Deserializes byte array to XmlRequest. Assumes not length prefix.
/// </summary>
/// <param name="ba"></param>
/// <returns></returns>
public static XmlReply FromBytes(byte[] ba)
{
XmlReply xr = null;
MemoryStream ms = new MemoryStream(ba);
xr = (XmlReply)ser.Deserialize(ms);
return xr;
}
 
Back
Top