最后,我对自动更新进行了编码。该文件将添加到数据库中。但我无法更新第0列中的文件名。它只提供第一次文件名。当我将文件名更改为其他不在column0中给出的内容时。
我只需输入part
中的特定文件名。我不知道如何修剪文件名并在列0中给出。请帮帮我。
此外,我发现了一个重要的错误..当我使用按钮将文件从单元格[1]移动到单元格[2]时。整个文件被移动到单元格[2]。所以那个时候文件观察者正在寻找那个特定文件夹并理解,文件被删除..所以代码将删除数据库..我不想这样做..我怎么处理这种情况..
请帮帮我
我自动更新的代码段:
namespace shell_FileSystemWatcher
{
public partial class Form1 : Form
{
public string partKey = "";
public Form1()
{
InitializeComponent();
}
/// <summary>
/// Create a new object of FileSystemWatcher
/// </summary>
FileSystemWatcher watcher = new FileSystemWatcher();
/// <summary>
/// Initialize the
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
ListBox listbox1 = new ListBox();
private void Form1_Load_1(object sender, EventArgs e)
{
///Creating listbox in current form
this.Controls.Add(listbox1);
listbox1.Size = new Size(500, 200);
listbox1.Location = new Point(0, 0);
listbox1.Visible = true;
///Assigning some properties to FileSystemWatcher class object
///Assign the path
watcher.Path = @"C:\user\elec\copy";
///Assign the filters as your requirement
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
///Handle the events that will be called when any file will be changed in the given folder path.
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
///Enabling the event call
watcher.EnableRaisingEvents = true;
///Initializing delegate that we have created to update UI control outside the current thread
addItemInList = new delAddItem(this.AddString);
}
// Define the event handlers.
private void OnChanged(object source, FileSystemEventArgs e)
{
FileInfo file = new FileInfo(e.FullPath);
Console.WriteLine(file.Name);
switch (e.ChangeType)
{
case WatcherChangeTypes.Created:
//Insert file in database
this.Invoke(addItemInList, "File: " + e.FullPath + " Created");
{
SqlConnection con = new SqlConnection(@"Data Source=Stacy492\SQLEXPRESS;Initial Catalog=cndb;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand(@"INSERT INTO cncinfo (part,draftpath) VALUES ('" + file.Name + "','" + e.FullPath + "') ", con);
cmd.ExecuteNonQuery();
con.Close();
}
break;
case WatcherChangeTypes.Deleted:
//remove file from database
this.Invoke(addItemInList, "File: " + e.FullPath + " Deleted");
{
SqlConnection con = new SqlConnection(@"Data Source=Stacy492\SQLEXPRESS;Initial Catalog=cndb;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand(@"delete cncinfo where part='" + file.Name + "' ;", con);
cmd.ExecuteNonQuery();
con.Close();
}
break;
case WatcherChangeTypes.Changed:
///if you are storing file in database(not file name whole file in binary format)
///then you can update the file in database here
///this event will be fired when any data has changed in the file.
this.Invoke(addItemInList, "File: " + e.FullPath + " Changed");
{
SqlConnection con = new SqlConnection(@"Data Source=Stacy492\SQLEXPRESS;Initial Catalog=cndb;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand(@"update cncinfo set part='" + NotifyFilters.FileName
+ "',draftpath='" + e.FullPath + "' where part='" + file.Name + "'", con);
cmd.ExecuteNonQuery();
con.Close();
this.Validate();
}
break;
}
}
private void OnRenamed(object source, RenamedEventArgs e)
{
FileInfo file = new FileInfo(e.FullPath);
Console.WriteLine(file.Name);
//Update then old filename with new here
this.Invoke(addItemInList, string.Format("File: {0} renamed to {1}", e.OldFullPath, e.FullPath));
{
SqlConnection con = new SqlConnection(@"Data Source=Stacy492\SQLEXPRESS;Initial Catalog=cndb;Integrated Security=True");
con.Open();
//s.debasish79@gmail.com
SqlCommand cmd = new SqlCommand(@"update cncinfo set part='" + file.Name
+ "',draftpath='" + e.FullPath + "' where part='" + file.Name + "'", con);
cmd.ExecuteNonQuery();
con.Close();
this.Validate();
}
}
#region We are creating delegate to update the value in Windows Form Control
/// <summary>
/// We are using delegate invoke method to update the UI control outside the current thread.
/// otherwise it will throws the error.
/// you can also use this method to update the cell value in datagridview
/// </summary>
/// <param name="_string"></param>
private delegate void delAddItem(string _string);
private delAddItem addItemInList;
private void AddString(string _string)
{
listbox1.Items.Add(_string);
}
#endregion
}
}
答案 0 :(得分:0)
要获取文件名,这将帮助您!!!!
FileInfo file = new FileInfo(e.FullPath);
Console.WriteLine(file.Name);