2014-09-07 2 views
2

마지막으로 자동 업데이트를 코딩했습니다. 파일이 데이터베이스에 추가됩니다. 하지만 난 열 0에서 파일 이름을 업데이트 할 수 없습니다. 그것은 단지 처음으로 파일 이름을 제공하고 있습니다. 파일 이름을 column0에 지정하지 않은 다른 것으로 변경합니다.파일 워처에서 파일 이름을 업데이트하는 방법

part에 특정 파일 이름 만 입력하면됩니다. 파일 이름을 다듬어서 column0을주는 방법을 모른다. 제발 도와주세요.

또한 중요한 버그를 발견했습니다. 버튼을 사용하여 셀 [1]에서 셀 [2]로 파일을 이동할 때. 전체 파일이 [2] 셀로 이동됩니다. 그래서 그 시간에 파일 감시인은 특정 폴더를 찾고 이해합니다. 파일이 삭제됩니다. 코드가 데이터베이스를 삭제할 것입니다.이 작업을 수행하고 싶지 않습니다. 어떻게 처리 할 수 ​​있습니까? ..

자동 upadation에 대한

내 코드를 좀 도와주십시오

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(); 
      //[email protected] 
      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 

} 

}

+1

파일 이동 여부를 확인할 수있는 이벤트가 없습니다. 파일을 다른 위치로 수동으로 이동하면 Windows는 처음에 파일을 새 폴더에 복사하고 이전 위치에서 파일을 삭제합니다. – Shell

+0

@Shell하지만 .. 어떻게이 상황을 처리 할 수 ​​있습니까? 내가 그 버튼을 누르면 .. 파일 작업자가 삭제 작업을 원합니다. –

+0

@Shell 또한 파일 이름을 가져 와서 column0에서 업데이트 할 수 있습니까? 나는 그것을 할 수 없다 .. 단지 내가 처음으로 새 문서를 만들 때 .. 나는 파일 이름을 얻고있다. 휴식 시간 .. 그것의 복용하지 말아라. .. 제발 나를 도와주세요. –

답변

0

이 당신을 도울 것입니다 파일 이름을 얻으려면 !!!!

FileInfo file = new FileInfo(e.FullPath); 
    Console.WriteLine(file.Name); 
+0

나는 이것을 처음으로했습니다. 파일 이름을 얻는 것 !!!! –

관련 문제