473,608 Members | 1,811 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to use an XML config file in Windows Service

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\syst em32\.

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

Thanks,
SP
Nov 16 '05 #1
5 12282
You can use the System.Configur ation.Configura tionSettings.Ap pSettings
to store the settings in a file.

Lowell

SP wrote:
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\syst em32\.

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

Thanks,
SP

Nov 16 '05 #2
SP
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
"Lowell Heddings" <lo****@mindjun ction.com> wrote in message
news:O9******** *****@TK2MSFTNG P10.phx.gbl...
You can use the System.Configur ation.Configura tionSettings.Ap pSettings
to store the settings in a file.

Lowell

SP wrote:
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\syst em32\.

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

Thanks,
SP

Nov 16 '05 #3
SP wrote:
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
Nov 16 '05 #4
"SP" <EA*@sp.com> wrote in message
news:eP******** ******@TK2MSFTN GP09.phx.gbl...
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\syst em32\.

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\Syst em32.
You simply need to specify the full path to the XML file. You can get the
executables directory from:

System.AppDomai n.CurrentDomain .BaseDirectory

Nov 16 '05 #5
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(t ypeof(XmlReply) );

// Methods in your class

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

public static XmlReply FromXmlString(s tring xmlString)
{
if ( xmlString == null )
throw new ArgumentNullExc eption("xmlStri ng");

XmlReply xr = null;
using (StringReader sr = new StringReader(xm lString))
{
xr = (XmlReply)ser.D eserialize(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(MemoryStr eam ms = new MemoryStream())
{
if ( prefixLen )
ms.Position = 4;

ser.Serialize(m s, this, NS);

if ( prefixLen )
{
if ( ms.Length > uint.MaxValue )
throw new ArgumentExcepti on("Serialized class bigger then
uint.MaxValue") ;
uint len = (uint)ms.Length ;
byte[] lenBytes = BitConverter.Ge tBytes(len);
ms.Position = 0;
ms.Write(lenByt es, 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.D eserialize(ms);
return xr;
}

--
William Stacey, MVP
http://mvp.support.microsoft.com

"SP" <EA*@sp.com> wrote in message
news:u#******** ******@TK2MSFTN GP09.phx.gbl...
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
"Lowell Heddings" <lo****@mindjun ction.com> wrote in message
news:O9******** *****@TK2MSFTNG P10.phx.gbl...
You can use the System.Configur ation.Configura tionSettings.Ap pSettings
to store the settings in a file.

Lowell

SP wrote:
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\syst em32\.

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

Thanks,
SP



Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
6742
by: | last post by:
Hi, I have a Windows Service that uses a referenced dotnet-dll. In my dll I set some public string to a stringvalue that I get from reading a xml-file(my config file). It works fine if I use it in a vb-form. But since Windows Services ? executes? in windows\system32 I can´t locate my xml-config- file. I don´t want to set different "compiling-statements" such as :
2
1822
by: bmcelhany | last post by:
Hi all, I have a solution with 4 projects: a class library, a windows app, a windows service, and an installer project. The class library handles all communication with the database and its classes are used by both the application and the service. I have stored the database connection string in an app.config file located in the class library project. Everything was working fine on my dev box, but when I migrated to the QA box, I...
3
9506
by: Daniel Billingsley | last post by:
The documentation on this subject seems to give you 98% of what you need to know. From various sources, I have compiled an understanding that this should work: <configuration> <system.diagnostics> <switches> <add name="clientswitch" value="3" /> </switches> <trace autoflush="true" indentsize="0">
0
1090
by: billym | last post by:
Is it possible to dynamically update a windows service app.config file and reflect it within the application without having to do a restart of the service? Perhaps through an interface within the service itself?
4
22520
by: PeterW | last post by:
I have a Windows service that needs to get some values from a config file. I place the config file for the service in the System32 directory. I do not get the values using the usual ConfigurationSettings.AppSettings Any tips please cheers -- PeterW
3
8148
by: Martin Arvidsson, Visual Systems AB | last post by:
Hi! I created a .settings file and added some values. All values are user set defined. When i load my windows service i can read from the .config file. But, i can set a value and save to the file. What am i missing? Are there any security issues?
0
8169
by: Lucky | last post by:
Hi guys, I've got a problem under reading settlings from App.config file. I've used System.Configuration to reach to the "User Settings" and "Application Settings" sections. the problem is now I'm not able read the property defined under this section. those are like this User Settings -Testing Application Settings -Test
0
3039
by: Sparkiee | last post by:
Hi, A lil bit of help required from you guys, I am trying to make a windows service that will also focus on globalization, the problem i am facing is when i am trying to put globalization code in app.config the windows service throws an error its probably because of using the <system.webin app.config that contains the globalization parameters, copied and pasted from one of my asp.net website where i already used the globalization and i...
14
5587
by: Peter | last post by:
..NET 3.5 I have a Windows Service application and it does remoting, but when a client incounters an error the client get the following error message "Server encountered an internal error. For more information, turn off customErrors in the server's .config file." Where do I turn this off
1
7974
by: TechBabu | last post by:
I'm trying to read data from config file in windows service in OnStart(), but it is throwing error "Object reference not set to an instance of an object". Is there any seeting required to use config file in Windows Service. my config file name as <appName>.exe.config. Even I placed it in the System32 folder also. Plz help me out how to handle config file in Windows Service. I'm using VS2008, C#. Thanks in advance.
0
8011
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8503
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8488
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8160
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6017
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3972
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4036
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2479
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1339
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.