Black Shadow
شنبه 28 فروردین 1389, 11:57 صبح
با سلام.
برای اینکه تغییرات یک فولدر مانند کپی شدن یا پاک شدن و ... فایل توش رو بشه نمایش داد میشه از کنترلر FileSystemWatcher استفاده کرد تا تغییرات رو توی یک مثلا ListBox نمایش بشده و من هم دقیقا میخوام این اتفاق بیفته اما به یه مشکل بر میخورم! مشکل اینجاست که اگر چندین فایل رو با هم توی فولدری که داره Watch میشه کپی کنیم فقط اضافه شدن یکی رو توی ListBox نشون میده و میگه فقط یک فایل با نام فلان اضافه شد اما این درست نیست! من میخوام اگر مثلا 20 فایل همزمان کپی شد یا پاک شد یا ساخته شد هر 20 تا رو توی ListBox نمایش بده! کدی که من برای این کار نوشتم اینه:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using projectclasses;
using Searching.Properties;
using System.Configuration;
namespace Searching.Search_Forms
{
public partial class watchFolder : Form
{
public watchFolder()
{
InitializeComponent();
}
private void watchFolder_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
folderBrowserDialog1.ShowDialog();
label2.Visible = true;
SelectedFolderPath.Visible = true;
SelectedFolderPath.Text = folderBrowserDialog1.SelectedPath;
}
private void Watch_btn_Click(object sender, EventArgs e)
{
Settings my= new Settings();
string connectionstring = my.searchConnectionString;
// string cnString = ConfigurationManager.ConnectionStrings["DigiBrd"].ConnectionString;
// ConfigurationSettings.AppSettings("Conn");
// string conn= ConfigurationSettings.AppSettings("conn");
// System.Configuration.ConfigurationManager.AppSetti ngs["YOUR_KEY_STRING"];
fileSystemWatcher1.EnableRaisingEvents = true;
fileSystemWatcher1.Path = SelectedFolderPath.Text;
//FileSystemWatcher fsw = new FileSystemWatcher(SelectedFolderPath.Text);
// fsw.EnableRaisingEvents = true;
fileSystemWatcher1.Filter = "*.htm*";
//fileSystemWatcher1.Changed += new FileSystemEventHandler(OnChanged);
}
private void OnChanged(object sender, FileSystemEventArgs e)
{
string[] Tag_name = { "foldername", "filename", "title","rooznameh","tarikh",
"titr","matn","zaban_rasaneh", "keshvar_rasaneh","noa_rasaneh","noa_rasaneh_khabar",
"noa_rasane_nobati","tabagheh_bandi","tolidkonnadeh","arzyabi","adress_internet" };
//--------------------------------------------------------------------
string[] files = Directory.GetFiles(e.FullPath.Substring(0, (e.FullPath.Length) - (e.Name.Length)));
CountOfFilesLable.Text = files.Length.ToString();
if (e.FullPath != null)
{
Settings my = new Settings();
string connectionstring = my.searchConnectionString;
//var MyList =
// from f in Directory.GetFiles((e.FullPath.Substring(0, (e.FullPath.Length) - (e.Name.Length))))
// where f.Contains("htm")
// select f;
ManipulateDatabase m = new ManipulateDatabase(connectionstring);
m.InsertPath(folderBrowserDialog1.SelectedPath, e.Name);
ReadFile rf = new ReadFile();
TagFinder tagFinder=new TagFinder();
Modify modify = new Modify();
string StringedFile;
//foreach (char t in e.FullPath)
//{
StringedFile = rf.Read(e.FullPath);
for (int i = 2; i < Tag_name.Length; i++)
{
string findedtag= tagFinder.TagContent(StringedFile, Tag_name[i]);
// convert all strings to unicode and normalize all
//findedtag= modify.NormalizeWords(findedtag);
if (Tag_name[i] == "tarikh")
{
findedtag = modify.NormalizeDate(findedtag);
}
m.UpdateFilesTagDb(folderBrowserDialog1.SelectedPa th, e.Name,Tag_name[i],findedtag);
//}
//if(progressBar1.Value<100)
//progressBar1.Value = progressBar1.Value +(int)(100/files.Length);
// count++;
}
//MessageBox.Show(" فایل به پایگاه داده اضافه شد " + count.ToString());
// MessageBox.Show(" فایل به پایگاه داده اضافه شد ");
}
// MessageBox.Show(" فایل به پایگاه داده اضافه شد ");
}
}
}
فکر میکنم مشکل از اون جایی هست که پر رنگ کردمش! اگر کسی کمک کنه خیلی ممنون میشم!
برای اینکه تغییرات یک فولدر مانند کپی شدن یا پاک شدن و ... فایل توش رو بشه نمایش داد میشه از کنترلر FileSystemWatcher استفاده کرد تا تغییرات رو توی یک مثلا ListBox نمایش بشده و من هم دقیقا میخوام این اتفاق بیفته اما به یه مشکل بر میخورم! مشکل اینجاست که اگر چندین فایل رو با هم توی فولدری که داره Watch میشه کپی کنیم فقط اضافه شدن یکی رو توی ListBox نشون میده و میگه فقط یک فایل با نام فلان اضافه شد اما این درست نیست! من میخوام اگر مثلا 20 فایل همزمان کپی شد یا پاک شد یا ساخته شد هر 20 تا رو توی ListBox نمایش بده! کدی که من برای این کار نوشتم اینه:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using projectclasses;
using Searching.Properties;
using System.Configuration;
namespace Searching.Search_Forms
{
public partial class watchFolder : Form
{
public watchFolder()
{
InitializeComponent();
}
private void watchFolder_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
folderBrowserDialog1.ShowDialog();
label2.Visible = true;
SelectedFolderPath.Visible = true;
SelectedFolderPath.Text = folderBrowserDialog1.SelectedPath;
}
private void Watch_btn_Click(object sender, EventArgs e)
{
Settings my= new Settings();
string connectionstring = my.searchConnectionString;
// string cnString = ConfigurationManager.ConnectionStrings["DigiBrd"].ConnectionString;
// ConfigurationSettings.AppSettings("Conn");
// string conn= ConfigurationSettings.AppSettings("conn");
// System.Configuration.ConfigurationManager.AppSetti ngs["YOUR_KEY_STRING"];
fileSystemWatcher1.EnableRaisingEvents = true;
fileSystemWatcher1.Path = SelectedFolderPath.Text;
//FileSystemWatcher fsw = new FileSystemWatcher(SelectedFolderPath.Text);
// fsw.EnableRaisingEvents = true;
fileSystemWatcher1.Filter = "*.htm*";
//fileSystemWatcher1.Changed += new FileSystemEventHandler(OnChanged);
}
private void OnChanged(object sender, FileSystemEventArgs e)
{
string[] Tag_name = { "foldername", "filename", "title","rooznameh","tarikh",
"titr","matn","zaban_rasaneh", "keshvar_rasaneh","noa_rasaneh","noa_rasaneh_khabar",
"noa_rasane_nobati","tabagheh_bandi","tolidkonnadeh","arzyabi","adress_internet" };
//--------------------------------------------------------------------
string[] files = Directory.GetFiles(e.FullPath.Substring(0, (e.FullPath.Length) - (e.Name.Length)));
CountOfFilesLable.Text = files.Length.ToString();
if (e.FullPath != null)
{
Settings my = new Settings();
string connectionstring = my.searchConnectionString;
//var MyList =
// from f in Directory.GetFiles((e.FullPath.Substring(0, (e.FullPath.Length) - (e.Name.Length))))
// where f.Contains("htm")
// select f;
ManipulateDatabase m = new ManipulateDatabase(connectionstring);
m.InsertPath(folderBrowserDialog1.SelectedPath, e.Name);
ReadFile rf = new ReadFile();
TagFinder tagFinder=new TagFinder();
Modify modify = new Modify();
string StringedFile;
//foreach (char t in e.FullPath)
//{
StringedFile = rf.Read(e.FullPath);
for (int i = 2; i < Tag_name.Length; i++)
{
string findedtag= tagFinder.TagContent(StringedFile, Tag_name[i]);
// convert all strings to unicode and normalize all
//findedtag= modify.NormalizeWords(findedtag);
if (Tag_name[i] == "tarikh")
{
findedtag = modify.NormalizeDate(findedtag);
}
m.UpdateFilesTagDb(folderBrowserDialog1.SelectedPa th, e.Name,Tag_name[i],findedtag);
//}
//if(progressBar1.Value<100)
//progressBar1.Value = progressBar1.Value +(int)(100/files.Length);
// count++;
}
//MessageBox.Show(" فایل به پایگاه داده اضافه شد " + count.ToString());
// MessageBox.Show(" فایل به پایگاه داده اضافه شد ");
}
// MessageBox.Show(" فایل به پایگاه داده اضافه شد ");
}
}
}
فکر میکنم مشکل از اون جایی هست که پر رنگ کردمش! اگر کسی کمک کنه خیلی ممنون میشم!