Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new in C# development,
I want to know about Windows services,
Actually I want to read my XML file and store its value(Date n Time) in variable and then according to that value want to run my batch file on that time even if my application is not open.

Is this possible in C# Windows services in Visual studio 2008 ???
Kindly give me your guidance..

Basically i want to create one service which can perform following operation
1. Read XML file
2. Store Value (value indicate some Date and Time)
3. Run my batch file on that date and time.
Posted

1 solution

For running a batch file on some schedule, you can create your own service, but an already created existing service could suffice. It is already bundled with Windows and enabled; you can use it on different levels. It is called Window Task Scheduler, see http://en.wikipedia.org/wiki/Windows_Task_Scheduler[^].

First, you can schedule events using command-line utilities AT.EXE or CSHTASKS.EXE (which is replacing AT.EXE), see:
http://en.wikipedia.org/wiki/At_%28Windows%29[^],
http://en.wikipedia.org/wiki/Schtasks[^],
http://technet.microsoft.com/en-us/library/bb490866.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/bb736357%28v=vs.85%29.aspx[^].

And you also can use Window Task Scheduler API, please see:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa383614%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/aa383608%28v=vs.85%29.aspx[^].

To see how can you use it with .NET, see this CodeProject article: A New Task Scheduler Class Library for .NET[^].

Now, for reading XML, .NET .FCL provides different classes. Please see my short overview of them:


  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^].
  2. Use the classes System.Xml.XmlTextWriter and System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx[^], http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^].
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].


However, I doubt that you need to deal with XML files by yourself. Instead, you can use already available serialization. One of the best approaches is using Data Contract, the easiest to use but most robust way of serialization: http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

Please see my past answers:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^].

If, by some reason, you still need to learn Windows Services, please start here:
http://en.wikipedia.org/wiki/Windows_service[^],
http://technet.microsoft.com/en-us/library/cc783643%28v=ws.10%29.aspx[^],
http://msdn.microsoft.com/en-us/library/ms685141.aspx[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900