PDA

View Full Version : حرفه ای: مشکل با Watch کردن فولدر!!!



Black Shadow
شنبه 28 فروردین 1389, 10: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(" فایل به پایگاه داده اضافه شد ");
}
}
}


فکر میکنم مشکل از اون جایی هست که پر رنگ کردمش! اگر کسی کمک کنه خیلی ممنون میشم!

Black Shadow
شنبه 28 فروردین 1389, 13:41 عصر
من سریعا به راهنمایی یکی از مدیران نیاز دارم.
یه مقدار توضیح بیشتر بدم.
این برنامه قرار هست برخی اطلاعات رو از روی دیتابیس بخونه و هر تغییری توی فایلهای و فولدر ها ایجاد شد برامون توی یه ListBox نمایش بده ولی همونطور که گفتم فقط یکی از تغییرات رو نمایش میده! کدها رو دوباره چک کردم... نمیدونم دقیقا مشکل از کجا میتونه باشه!

ramin2nt2
شنبه 28 فروردین 1389, 16:53 عصر
سلام لطفا این سورس رو بررسی کنید
من امتحان کردم، مولتی فایل رو هم جواب داد





using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace csharp_WATCHER
{
class Program
{
static void Main(string[] args)
{

FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = "D:\\" ;

watcher.Created += new FileSystemEventHandler(watcher_changed);
watcher.Deleted += new FileSystemEventHandler(watcher_changed);

watcher.Renamed += new RenamedEventHandler (watcher_renamed);

watcher.EnableRaisingEvents = true ;

Console.ReadKey();

}

public static void watcher_changed(object sender, FileSystemEventArgs e)
{

Console.WriteLine("object {0} at: {1}", e.ChangeType, e.FullPath);
}

public static void watcher_renamed(object sender,RenamedEventArgs e)
{
Console.WriteLine("object renamed from {0} to {1}", e.OldName, e.Name);
}


}
}