1-) C# RMOS - Dosya dinleme
public void CreateFileWatcher(string path)
{
// Create a new FileSystemWatcher and set its properties.
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = path;
/* Watch for changes in LastAccess and LastWrite times, and
the renaming of files or directories. */
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
// Only watch text files.
watcher.Filter = "*.txt";
// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
// Begin watching.
watcher.EnableRaisingEvents = true;
}
// Define the event handlers.
private static void OnChanged(object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
}
private static void OnRenamed(object source, RenamedEventArgs e)
{
// Specify what is done when a file is renamed.
Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
}
2-) Dosya dinleme
using System;
using System.IO;
using System.Windows.Forms;
namespace dosyaListener
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
FileSystemWatcher watcher;
public void CreateFileWatcher(string path)
{
watcher = new FileSystemWatcher();
watcher.Path = path;
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Filter = "*.pdf";
watcher.Changed += new FileSystemEventHandler(OnChanged);
// watcher.Created += new FileSystemEventHandler(OnChanged);
// watcher.Renamed += new RenamedEventHandler(OnRenamed);
// watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
}
private void OnChanged(object source, FileSystemEventArgs e)
{
//MessageBox.Show("File: " + e.FullPath);
string dosyaYolu = e.FullPath.ToString();
Console.WriteLine(dosyaYolu);
pdfViewer1.DocumentFilePath = dosyaYolu;
}
private void Form1_Load(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false;//dinamik nesne eklemek için listeye
CreateFileWatcher(@"C:\Rmos Arsiv\1");
CreateFileWatcher(@"C:\Rmos Arsiv\2");
CreateFileWatcher(@"C:\Rmos Arsiv\3");
CreateFileWatcher(@"C:\Rmos Arsiv\4");
CreateFileWatcher(@"C:\Rmos Arsiv\5");
CreateFileWatcher(@"C:\Rmos Arsiv\6");
CreateFileWatcher(@"C:\Rmos Arsiv\7");
CreateFileWatcher(@"C:\Rmos Arsiv\8");
CreateFileWatcher(@"C:\Rmos Arsiv\9");
MessageBox.Show("Dinliyor");
}
/*
private static void OnRenamed(object source, RenamedEventArgs e)
{
// Specify what is done when a file is renamed.
Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
}
*/
}
}
Ramazan HABER
Software Developer
UYARI KODLARI BUNUN İÇİNE YAZ O ZAMAN BUNA GEREK KALMAZ
-> CheckForIllegalCrossThreadCalls = false;
private void OnChanged(object source, FileSystemEventArgs e)
{
this.Invoke(new MethodInvoker(() =>
{
string dosyaYolu = e.FullPath.ToString();
Console.WriteLine(dosyaYolu);
DataRow dr = dt.NewRow();
dr["ad"] = "" + dosyaYolu;
dt.Rows.Add(dr);
gridControl1.DataSource = dt;
}));
}