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
Member1
NA
169
50.9k
How to read file and dispose streamReader object?
Nov 6 2015 1:08 AM
I am writing program which reads data from csv file when file update. Now i got simple error as shown following. i.e OnChanged event fires two or more times i want to execute it one time when file update.
public void GetData()
{
try
{
//for (int i = 0; i >= 0; i++)
//{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = filepath;
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
// Only watch text files.
watcher.Filter = "*.csv";
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
//}
}
catch { }
}
private void OnChanged(object source, FileSystemEventArgs e)
{
try
{
NetworkStream networkStream = clientSocket.GetStream();
line = new string[] { };
using (FileStream fs = new FileStream(e.FullPath, FileMode.Open, FileAccess.Read))
using (StreamReader sr = new StreamReader(fs))
{
line = sr.ReadLine().Split(',');
for (int p = 0; p < line.Length; p++)
{
b = Encoding.ASCII.GetBytes(line[p]);
networkStream.Write(b, 0, b.Length);
}
sr.Close();
sr.Dispose();
fs.Flush();
}
}
catch { }
}
Reply
Answers (
2
)
login problem on c-sharp corner
How to do FullColumn Selection on a DataGridView with SelectioMode set to CellSelect