I need a windows service that listens to a directory , if any xml file is
placed in the directory it reads it and calls a class and receives a
string.. this is the code I came up with. it works but it kills my processor
after a few min. what should i do??
Thanks
private void button6_Click(object sender, System.EventArgs e)
{
NewClaims.clsNewClaims objNewClaimI = new NewClaims.clsNewClaims();
while (true)
{
//Get the File in the directory
DirectoryInfo dir = new DirectoryInfo(@"C:\test4");
FileInfo[] xmlfiles = dir.GetFiles("*.xml");
foreach( FileInfo file in xmlfiles)
{
string strHMI="";
//Opens an existing UTF-8 encoded text file for reading.
StreamReader sr = File.OpenText(file.FullName);
string data= sr.ReadToEnd();
strHMI = objNewClaimI.ConvertNewClaimsXmlUICIToHMItxt(data) ;
int intExtension=file.Name.IndexOf(".");
FileInfo outputFile = new
FileInfo(@"c:\test5\"+file.Name.Substring(0,intExt ension)+".txt");
StreamWriter strWriter =new StreamWriter(File.Open(@"c:\test5\" +
file.Name.Substring(0,intExtension) +".txt",FileMode.Create,
FileAccess.Write ));
strWriter.Flush();
strWriter.Write(strHMI.ToString());
strWriter.Close();
sr.Close();
file.Delete();
}
}
} 5 1668
Hi CK
You should try using the FileSystemWatcher component. This does exactly what
you want. Your code seems to be in a tight loop, hence the problem! With the
FileSystemWatcher, you get a notification when a file addition (or
modification or deletion) occurs in the watched directory.
Try this URL for a useful article in MSDN magazine: http://msdn.microsoft.com/msdnmag/issues/01/07/vbnet/
HTH
Nigel Armstrong
"CK" wrote: I need a windows service that listens to a directory , if any xml file is placed in the directory it reads it and calls a class and receives a string.. this is the code I came up with. it works but it kills my processor after a few min. what should i do??
Thanks
private void button6_Click(object sender, System.EventArgs e)
{
NewClaims.clsNewClaims objNewClaimI = new NewClaims.clsNewClaims(); while (true)
{
//Get the File in the directory
DirectoryInfo dir = new DirectoryInfo(@"C:\test4");
FileInfo[] xmlfiles = dir.GetFiles("*.xml");
foreach( FileInfo file in xmlfiles)
{
string strHMI="";
//Opens an existing UTF-8 encoded text file for reading.
StreamReader sr = File.OpenText(file.FullName);
string data= sr.ReadToEnd();
strHMI = objNewClaimI.ConvertNewClaimsXmlUICIToHMItxt(data) ;
int intExtension=file.Name.IndexOf(".");
FileInfo outputFile = new FileInfo(@"c:\test5\"+file.Name.Substring(0,intExt ension)+".txt");
StreamWriter strWriter =new StreamWriter(File.Open(@"c:\test5\" + file.Name.Substring(0,intExtension) +".txt",FileMode.Create, FileAccess.Write ));
strWriter.Flush();
strWriter.Write(strHMI.ToString());
strWriter.Close();
sr.Close();
file.Delete();
}
}
}
I agree that FileSystemWatcher is what you want to use, but if that
doesn't work for you (whatever the reason) you could put this operation
in a separate thread and be sure to call Thread.Sleep() periodically.
I tried FileSystemWatcher but it wont work when I insert a file into the
directory. it works only when I change something in the file, delete the
file. its basicly changes within the directory not adding a file into the
directory.
I did try the treading and its better except I dont know where in the code i
should try the tread.sleep() method.
Thanks
"Nigel Armstrong" <Ni************@discussions.microsoft.com> wrote in
message news:5A**********************************@microsof t.com... Hi CK
You should try using the FileSystemWatcher component. This does exactly what you want. Your code seems to be in a tight loop, hence the problem! With the FileSystemWatcher, you get a notification when a file addition (or modification or deletion) occurs in the watched directory.
Try this URL for a useful article in MSDN magazine:
http://msdn.microsoft.com/msdnmag/issues/01/07/vbnet/
HTH
Nigel Armstrong
"CK" wrote:
I need a windows service that listens to a directory , if any xml file is placed in the directory it reads it and calls a class and receives a string.. this is the code I came up with. it works but it kills my processor after a few min. what should i do??
Thanks
private void button6_Click(object sender, System.EventArgs e)
{
NewClaims.clsNewClaims objNewClaimI = new NewClaims.clsNewClaims(); while (true)
{
//Get the File in the directory
DirectoryInfo dir = new DirectoryInfo(@"C:\test4");
FileInfo[] xmlfiles = dir.GetFiles("*.xml");
foreach( FileInfo file in xmlfiles)
{
string strHMI="";
//Opens an existing UTF-8 encoded text file for reading.
StreamReader sr = File.OpenText(file.FullName);
string data= sr.ReadToEnd();
strHMI = objNewClaimI.ConvertNewClaimsXmlUICIToHMItxt(data) ;
int intExtension=file.Name.IndexOf(".");
FileInfo outputFile = new FileInfo(@"c:\test5\"+file.Name.Substring(0,intExt ension)+".txt");
StreamWriter strWriter =new StreamWriter(File.Open(@"c:\test5\" + file.Name.Substring(0,intExtension) +".txt",FileMode.Create, FileAccess.Write ));
strWriter.Flush();
strWriter.Write(strHMI.ToString());
strWriter.Close();
sr.Close();
file.Delete();
}
}
}
using System;
using System.IO;
using System.Threading;
namespace FileWatcherThread
{
/// <summary>
/// Summary description for ConsoleFileWatcher.
/// </summary>
class ConsoleFileWatcher
{
public static bool Watch;
[STAThread]
static void Main(string[] args)
{
if (args.Length == 1)
{
string p = args[0];
if (Directory.Exists(p))
{
ConsoleFileWatcher.WatchDirectory(p, @"C:\Temp\HmiOutput");
}
else
{
Console.WriteLine("The path <" + p + "> does not exist or is not a
directory.");
}
}
else
{
Console.WriteLine("You did not provide a path to watch.");
}
}
public static void WatchDirectory(string sourcePath, string
targetPath)
{
ConsoleFileWatcher.Watch = true;
DirectoryInfo s = new DirectoryInfo(sourcePath);
DirectoryInfo t = new DirectoryInfo(targetPath);
Console.WriteLine("Watching directory <" + s.FullName + ">");
string p;
while (ConsoleFileWatcher.Watch)
{
foreach (FileInfo f in s.GetFiles("*.xml"))
{
p = Path.Combine(t.FullName, f.Name.Replace("xml", "txt"));
ConsoleFileWatcher.WriteTargetData(f, new FileInfo(p));
}
Thread.Sleep(1000);
}
}
public static void WriteTargetData(FileInfo source, FileInfo target)
{
Console.WriteLine("Converting <" + source.FullName + "> to <" +
target.FullName + ">");
StreamReader r = null;
string d = null;
try
{
r = File.OpenText(source.FullName);
d = r.ReadToEnd();
}
catch (Exception e)
{
Console.WriteLine("An exception occurred while reading the source
file.");
Console.WriteLine();
Console.WriteLine(e.ToString());
}
finally
{
if (r != null)
{
r.Close();
}
}
source.Delete();
if ((d == null) || (d == string.Empty))
{
Console.WriteLine("No source data was read.");
return;
}
else
{
d = ConsoleFileWatcher.ConvertNewClaimsXmlUICIToHMItxt (d);
}
StreamWriter w = null;
try
{
w = new StreamWriter(target.Create());
w.Write(d);
}
catch (Exception e)
{
Console.WriteLine("An exception occurred while writing the target
file.");
Console.WriteLine();
Console.WriteLine(e.ToString());
}
finally
{
if (w != null)
{
w.Close();
}
}
}
public static string ConvertNewClaimsXmlUICIToHMItxt(string data)
{
// do your conversion here
return data;
}
}
}
inside the while(true) loop.
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster http://blogs.msdn.com/nickmalik
Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"CK" <jo*****@hotmail.com> wrote in message
news:O6**************@TK2MSFTNGP14.phx.gbl... I tried FileSystemWatcher but it wont work when I insert a file into the directory. it works only when I change something in the file, delete the file. its basicly changes within the directory not adding a file into the directory. I did try the treading and its better except I dont know where in the code
i should try the tread.sleep() method.
Thanks "Nigel Armstrong" <Ni************@discussions.microsoft.com> wrote in message news:5A**********************************@microsof t.com... Hi CK
You should try using the FileSystemWatcher component. This does exactly what you want. Your code seems to be in a tight loop, hence the problem! With the FileSystemWatcher, you get a notification when a file addition (or modification or deletion) occurs in the watched directory.
Try this URL for a useful article in MSDN magazine:
http://msdn.microsoft.com/msdnmag/issues/01/07/vbnet/
HTH
Nigel Armstrong
"CK" wrote:
I need a windows service that listens to a directory , if any xml file
is placed in the directory it reads it and calls a class and receives a string.. this is the code I came up with. it works but it kills my processor after a few min. what should i do??
Thanks
private void button6_Click(object sender, System.EventArgs e)
{
NewClaims.clsNewClaims objNewClaimI = new NewClaims.clsNewClaims(); while (true)
{
//Get the File in the directory
DirectoryInfo dir = new DirectoryInfo(@"C:\test4");
FileInfo[] xmlfiles = dir.GetFiles("*.xml");
foreach( FileInfo file in xmlfiles)
{
string strHMI="";
//Opens an existing UTF-8 encoded text file for reading.
StreamReader sr = File.OpenText(file.FullName);
string data= sr.ReadToEnd();
strHMI = objNewClaimI.ConvertNewClaimsXmlUICIToHMItxt(data) ;
int intExtension=file.Name.IndexOf(".");
FileInfo outputFile = new FileInfo(@"c:\test5\"+file.Name.Substring(0,intExt ension)+".txt");
StreamWriter strWriter =new StreamWriter(File.Open(@"c:\test5\" + file.Name.Substring(0,intExtension) +".txt",FileMode.Create, FileAccess.Write ));
strWriter.Flush();
strWriter.Write(strHMI.ToString());
strWriter.Close();
sr.Close();
file.Delete();
}
}
}
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: belgiozen |
last post by:
Hi,
I have a working windows service,it is looking for files on the disk
and when some of the files are cupdated it calls an executable. But it
takes a lot of time(about 10 minutes) to run the...
|
by: Simon Harvey |
last post by:
Hi everyone,
I need to make a service that monitors a directory for changes in the files
contained within it. I have two questions:
1. I'm going to be using a FileSystemWatcher object to do...
|
by: Richard Steele |
last post by:
I have created a WinForm application that needs to be run as a windows
service (the PC is inaccesible by any user) i have successfully installed the
application as a windows service. When i start...
|
by: Scott Davies |
last post by:
Hi,
I'm looking for some help on a small program that I'm trying to develop in
VB.NET. I'm having trouble getting the code that I've written to work, can
anyone shed some light as to where I'm...
|
by: Scott Davies |
last post by:
Hi,
I'm looking for some help on a small program that I'm trying to develop in
VB.NET.
The program I'm trying to develop needs to be able to do the following:
- Select remote server
-...
|
by: Ger |
last post by:
I am having problems using VB.Net's Management base object on a
machine hosting Windows Server 2003. I am trying to set file
permissions from a Windows Service. These files may be loacted on a...
|
by: Andrew |
last post by:
Hey all,
Requesting help from the VB.Net gurus in here. I was given a task to write
a Windows Service (VB.Net) that would run an external program, and if that
program closed for any reason...
|
by: Charles Leonard |
last post by:
I am having yet another issue with Windows Server 2003. This time, the web
service (a file import web service) appears to run except for one odd
message: "ActiveX component can't create object". ...
|
by: =?Utf-8?B?dmlzaHJ1dGg=?= |
last post by:
Hi,
I have 2 applications running, one Windows application project and the other
windows services project.
I want to call my Windows application in my windows services.
I want to run them as...
|
by: dm3281 |
last post by:
I'm really starting to hate writing services -- or trying to, anyway.
Why do I need to rename my project to the service name?
Why do I need to set the "ServiceName" property to my service name?...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
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,...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
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...
| |