TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Ashutosh Parab
NA
3
754
Creating Service To Monitor And Read Contents Of File From A
Feb 11 2015 1:04 AM
Hi all,
am trying to create a service that will try to create a service that will monitor a folder. Whenever a new file gets created, I am trying to read the contents of new file and copy contents (with same file) at a new location.
The problem I am facing is that only first file that gets its name copied and a file created at a new location, but without any contents. The subsequent files do not get created at all.
My Services.cs looks like this:-
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
FileSystemWatcher Watcher = new FileSystemWatcher();
Watcher.Path = "C:\\logs\\BOR";
Watcher.IncludeSubdirectories = true;
Watcher.Created += new FileSystemEventHandler(Watcher_Changed);
Watcher.EnableRaisingEvents = true;
}
private static void Watcher_Changed(object sender, FileSystemEventArgs e)
{
var CopyContents = new Thread(() => ThreadProcedure(e));
CopyContents.IsBackground = true;
CopyContents.Start();
}
private static void ThreadProcedure(FileSystemEventArgs e)
{
Thread.Sleep(10000);
int counter = 0;
string line;
// Read the file and display it line by line.
System.IO.StreamReader file =
new System.IO.StreamReader(e.FullPath);
string testfilepath = "C:\\Project Tests\\testfile\\" + e.Name;
File.Create(testfilepath);
var log = new StreamWriter(testfilepath);
while ((line = file.ReadLine()) != null)
{
log.WriteLine(line);
counter++;
}
log.Close();
file.Close();
}
protected override void OnStop()
{
}
}
Reply
Answers (
1
)
How to create c#.net Architecture?
How can I get and set frequency of a voice in c#?