Groups | Blog | Home
all groups > dotnet xml > december 2004 >

dotnet xml : windows service help


CK
12/18/2004 7:43:37 AM
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,intExtension)+".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();

}


}

}

Nigel Armstrong
12/18/2004 9:31:04 AM
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

[quoted text, click to view]
Chris Bray
12/18/2004 10:44:45 AM
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.
CK
12/18/2004 4:04:12 PM
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" <NigelArmstrong@discussions.microsoft.com> wrote in
message news:5A4DC510-BC85-4FAE-AC25-7B2715010ADB@microsoft.com...
[quoted text, click to view]

Chris Bray
12/18/2004 4:46:43 PM
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;
}
}
}
Nick Malik [Microsoft]
12/25/2004 6:46:31 PM
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.
--
[quoted text, click to view]

AddThis Social Bookmark Button