2009-05-22 51 views
0

我想爲打開文件並查看e:\ test \ test.doc中的文件編寫一個程序,然後我想在單獨的文件文件跟蹤中顯示路徑。文本。在c中顯示/查找打開的文件路徑#

如果我打開F中的文件:\ doc.pdf輸出應該是:

the file in "e:\test.doc" is opened on 2.30 am 
the file in "f:\doc.pdf" is opened on 8.30 am like wise 

我怎樣寫的程序在C#?

+0

你不確定哪一點?打開/寫入文件的代碼?你的程序應該採用的設計/步驟?這是用於控制檯應用程序還是winforms? – mundeep 2009-05-22 06:13:37

回答

1

很快...... 你可以使用opendialog modalbox並且可以將路徑保存爲opendialogObject.path和DateTime.Now()的當前時間部分,並且可以將其顯示出來。 對不起,如果不幫助

0

using System.IO;

路徑字符串= 「E:\測試\ test.txt的」

//保存路徑字符串

的FileStream文件=新的FileStream(路徑的FileMode,FileAccess的); //打開Test.doc的文件

//執行文件操作

file.Close();

//打開路徑文件

的FileStream pathfile =新的FileStream( 「filetracing.txt」 的FileMode,FileAccess的);

StreamWriter sw = new StreamWriter(pathfile);

sw.Write(path); //路徑字符串將寫入您的文件tracing.txt中

sw.Close(); pathfile.Close();

1

您的意思是說您希望應用程序在訪問c:\上的任何文件時寫入日誌文件

如果是這樣,那麼有一個涉及System.IO.FileSystemWatcher類的可能解決方案。 (我鏈接到MSDN文檔,但由於我是新來的,我也不能)

FileSystemWatcher類更意在觀看了改變到文件系統,而不是任何直接的文件系統訪問。 因此,使用此解決方案僅限於觀察對LastAccessed所做的更改 - 並非總是進行更新,因此應嚴格審覈原因。

這裏有一個快速的代碼使用FileSystemWatcher的監視上次訪問的變化證明:

 using (var w = new System.IO.FileSystemWatcher("c:\\")) 
     { 
      w.IncludeSubdirectories = true; 
      w.NotifyFilter = NotifyFilters.LastAccess; 
      w.Changed += (object sender, FileSystemEventArgs e) => 
         { 
          Console.WriteLine("{0} {1} at {2}", Path.Combine(e.FullPath, e.Name), e.ChangeType, DateTime.Now); 
         }; 

      w.EnableRaisingEvents = true; 
      Console.WriteLine("Press Enter to exit"); 
      Console.Read(); 
     } 

注:

  • 這得到當任何文件有它LastAccessed屬性更新事件 - 包括文件創建和修改。

  • 如果您登錄到您正在監控的同一個驅動器,您將獲得自己的寫入事件 - 所以不要忘記對它們進行過濾。