2012-05-01 55 views
0

我開發了一個asp.net c#filewatcher程序來監視一個文件夾。如何在filewatcher中定義窗口登錄?

如果有任何修改過的或新創建的文件,程序會將這些文件複製到另一個窗口服務器共享文件夾,例如\ 192.168.12.12 \共享

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 

using System.IO; 
using System.Diagnostics; 
using System.Threading; 
using System.Net; 
using System.Globalization; 
using System.Text.RegularExpressions; 

namespace FileMonitor 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 

      fileWatcher.Path = @txtPath.Text; 
      fileWatcher.Filter = txtFilter.Text; 
      fileWatcher.IncludeSubdirectories = chkSubdirectories.Checked; 
     } 

     DateTime lastRead = DateTime.MinValue; 

     private void fileWatcher_Changed(object sender, System.IO.FileSystemEventArgs e) 
     { 
      DateTime lastWriteTime = File.GetLastWriteTime(e.FullPath); 

      if (lastWriteTime != lastRead) 
      { 


       txtLog.Text += e.ChangeType + ": " + e.FullPath + "\r\n"; 
       txtLog.Focus(); 
       txtLog.Select(txtLog.TextLength, 0); 
       txtLog.ScrollToCaret(); 

       try 
       { 
        string myPath = e.FullPath; 
        string myFile = e.Name; 

        System.IO.FileInfo myFileInfo = new System.IO.FileInfo(myFile); 

        string myAttibs = myFileInfo.Attributes.ToString(); 

        System.IO.File.Copy(myPath, @"\\192.168.12.12\\shared\\" + myFile, true); 

        lastRead = lastWriteTime; 

       } 
       catch (System.IO.IOException ex) 
       { 
        System.IO.IOException myex = ex; 
       } 
       catch (System.Exception ex) 
       { 
        System.Exception myex = ex; 
       } 

      } 
     } 

     private void fileWatcher_Created(object sender, System.IO.FileSystemEventArgs e) 
     { 
      DateTime lastWriteTime = File.GetLastWriteTime(e.FullPath); 

      if (lastWriteTime != lastRead) 
      { 


       txtLog.Text += e.ChangeType + ": " + e.FullPath + "\r\n"; 
       txtLog.Focus(); 
       txtLog.Select(txtLog.TextLength, 0); 
       txtLog.ScrollToCaret(); 

       try 
       { 
        string myPath = e.FullPath; 
        string myFile = e.Name; 

        System.IO.FileInfo myFileInfo = new System.IO.FileInfo(myFile); 

        string myAttibs = myFileInfo.Attributes.ToString(); 

        System.IO.File.Copy(myPath, @"\\192.168.12.12\\shared\\" + myFile, true); 

        lastRead = lastWriteTime; 

       } 
       catch (System.IO.IOException ex) 
       { 
        System.IO.IOException myex = ex; 
       } 
       catch (System.Exception ex) 
       { 
        System.Exception myex = ex; 
       } 

      } 
     } 

    } 
} 

但是我發現,我需要在運行filewatcher程序之前曾經訪問共享文件夾(如類型\ 192.168.12.12 \在資源管理器窗口共享,它會提示我輸入登錄名和密碼),否則程序無法複製到共享文件夾。

如何在我的程序中傳遞此登錄名和密碼以便它可以訪問共享文件夾?

謝謝。

+0

在使用頻繁的系統上,FileWatcher將報告「文件夾」更改,而不是文件更改。您不能使用FullPath來檢測確切的更改。您需要創建一個文件列表並手動檢測差異。 – CodingBarfield

回答

0

通常的做法是將共享文件夾的權限授予運行此類型複製代碼的用戶。

如果你不能這樣做,你需要使用Win32函數NetUseAddWNetAddConnection2爲了驗證共享(鏈接指向pinvoke.net,顯示如何在C#中使用這些函數)。