none
c# txt讀檔 RRS feed

  • 問題

  • 請問一下

    剛翻了下舊文章 好像沒有主要再提到預防無檔案的問題

    例如 我的pictureBox 在載入圖片時 我會先判斷路徑的index是否正確

    預防錯誤載入時程式擋掉

    那txt檔在載入的時候 有可以預防的方法嗎?

    我的txt檔數量會有更動 所以 我不太知道該怎樣預防使用者在載入的時候 沒有打到正確的字樣

    而載入錯誤

    我想做類似資料庫的東西

    想說只是個小東西 我要做新增跟查詢 應該是不用用到資料庫去做@@"!~

    2012年8月15日 上午 02:18

解答

  • namespace CSharp
    {
    
        using System;
        using System.IO;
    
        class Program
        {
            static void Main(string[] args)
            {
                //  Create a FileSystemWatcher to monitor all files on drive C.
                FileSystemWatcher fsw = new FileSystemWatcher("Screen\\");
    
                //  Watch for changes in LastAccess and LastWrite times, and
                //  the renaming of files or directories. 
                fsw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
                    | NotifyFilters.FileName | NotifyFilters.DirectoryName;
    
                fsw.Filter = "*.txt";
                //  Register a handler that gets called when a 
                //  file is created, changed, or deleted.
                fsw.Changed += new FileSystemEventHandler(OnChanged);
    
                fsw.Created += new FileSystemEventHandler(OnChanged);
    
                fsw.Deleted += new FileSystemEventHandler(OnChanged);
    
                //  Register a handler that gets called when a file is renamed.
                fsw.Renamed += new RenamedEventHandler(OnRenamed);
    
                //  Register a handler that gets called if the 
                //  FileSystemWatcher needs to report an error.
                fsw.Error += new ErrorEventHandler(OnError);
    
                //  Begin watching.
                fsw.EnableRaisingEvents = true;
    
                Console.WriteLine("Press \'Enter\' to quit the sample.");
                Console.ReadLine();
    
    
            }
    
            //  This method is called when a file is created, changed, or deleted.
            private static void OnChanged(object source, FileSystemEventArgs e)
            {
                //  Show that a file has been created, changed, or deleted.
                WatcherChangeTypes wct = e.ChangeType;
               if(wct==WatcherChangeTypes.Created)
               {
                   Console.WriteLine("FileCreated:"+e.FullPath);
               }
            }
    
            //  This method is called when a file is renamed.
            private static void OnRenamed(object source, RenamedEventArgs e)
            {
                //  Show that a file has been renamed.
                WatcherChangeTypes wct = e.ChangeType;
                Console.WriteLine("File {0} {2} to {1}", e.OldFullPath, e.FullPath, wct.ToString());
            }
    
            //  This method is called when the FileSystemWatcher detects an error.
            private static void OnError(object source, ErrorEventArgs e)
            {
                //  Show that an error has been detected.
                Console.WriteLine("The FileSystemWatcher has detected an error");
                //  Give more information if the error is due to an internal buffer overflow.
                if (e.GetException().GetType() == typeof(InternalBufferOverflowException))
                {
                    //  This can happen if Windows is reporting many file system events quickly 
                    //  and internal buffer of the  FileSystemWatcher is not large enough to handle this
                    //  rate of events. The InternalBufferOverflowException error informs the application
                    //  that some of the file system events are being lost.
                    Console.WriteLine(("The file system watcher experienced an internal buffer overflow: " + e.GetException().Message));
                }
            }
    
        }
    }

    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处

    2012年8月15日 上午 04:52

所有回覆

  • >>我的pictureBox 在載入圖片時 我會先判斷路徑的index是否正確

    尝试使用System.IO.File.Exists去检测路径是否正确(文件是否可以找到)

    >>那txt檔在載入的時候 有可以預防的方法嗎?

    恐怕你要弄一个文件监视工具(参考:http://msdn.microsoft.com/zh-tw/library/system.io.filesystemwatcher.aspx)。


    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处

    2012年8月15日 上午 02:24
  • 我目前是以此網站的方法去做 http://www.nmvs.info/contents/161/7139.html

    public void CreateWatcher()
            {
                //Create a new FileSystemWatcher.
                FileSystemWatcher watcher = new FileSystemWatcher();
                //Set the filter to only catch TXT files.  
                watcher.Filter = "TET.txt";
                //Subscribe to the Created event.  
                watcher.Created += new FileSystemEventHandler(watcher_FileCreated);
                //Set the path to C:Temp  
                watcher.Path = @"screen";
                //Enable the FileSystemWatcher events.  
                watcher.EnableRaisingEvents = true;
            }
            void watcher_FileCreated(object sender, FileSystemEventArgs e)
            {
                //A new .TXT file has been created in C:Temp  
                label2.Text = "A new *.txt file has been created!";
            }
    以上是我的程式碼

    但是我實做出來後 我的label並沒有印出字樣

    PS. 那我又該如何去判斷 我到底有沒有這檔案在呢?

    2012年8月15日 上午 03:10
  • >>我的pictureBox 在載入圖片時 我會先判斷路徑的index是否正確

    尝试使用System.IO.File.Exists去检测路径是否正确(文件是否可以找到)

    >>那txt檔在載入的時候 有可以預防的方法嗎?

    恐怕你要弄一个文件监视工具(参考:http://msdn.microsoft.com/zh-tw/library/system.io.filesystemwatcher.aspx)。


    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处

    還是說 他是 沒有找到就會不顯示嗎?

    2012年8月15日 上午 03:11
  • >>我的pictureBox 在載入圖片時 我會先判斷路徑的index是否正確

    尝试使用System.IO.File.Exists去检测路径是否正确(文件是否可以找到)

    >>那txt檔在載入的時候 有可以預防的方法嗎?

    恐怕你要弄一个文件监视工具(参考:http://msdn.microsoft.com/zh-tw/library/system.io.filesystemwatcher.aspx)。


    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处


    忘了說 screen 是我在 Debug 底下的資料夾 這樣設路徑 跟尋找的檔案名稱 應該沒錯吧@@"
    2012年8月15日 上午 03:13
  • namespace CSharp
    {
    
        using System;
        using System.IO;
    
        class Program
        {
            static void Main(string[] args)
            {
                //  Create a FileSystemWatcher to monitor all files on drive C.
                FileSystemWatcher fsw = new FileSystemWatcher("Screen\\");
    
                //  Watch for changes in LastAccess and LastWrite times, and
                //  the renaming of files or directories. 
                fsw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
                    | NotifyFilters.FileName | NotifyFilters.DirectoryName;
    
                fsw.Filter = "*.txt";
                //  Register a handler that gets called when a 
                //  file is created, changed, or deleted.
                fsw.Changed += new FileSystemEventHandler(OnChanged);
    
                fsw.Created += new FileSystemEventHandler(OnChanged);
    
                fsw.Deleted += new FileSystemEventHandler(OnChanged);
    
                //  Register a handler that gets called when a file is renamed.
                fsw.Renamed += new RenamedEventHandler(OnRenamed);
    
                //  Register a handler that gets called if the 
                //  FileSystemWatcher needs to report an error.
                fsw.Error += new ErrorEventHandler(OnError);
    
                //  Begin watching.
                fsw.EnableRaisingEvents = true;
    
                Console.WriteLine("Press \'Enter\' to quit the sample.");
                Console.ReadLine();
    
    
            }
    
            //  This method is called when a file is created, changed, or deleted.
            private static void OnChanged(object source, FileSystemEventArgs e)
            {
                //  Show that a file has been created, changed, or deleted.
                WatcherChangeTypes wct = e.ChangeType;
               if(wct==WatcherChangeTypes.Created)
               {
                   Console.WriteLine("FileCreated:"+e.FullPath);
               }
            }
    
            //  This method is called when a file is renamed.
            private static void OnRenamed(object source, RenamedEventArgs e)
            {
                //  Show that a file has been renamed.
                WatcherChangeTypes wct = e.ChangeType;
                Console.WriteLine("File {0} {2} to {1}", e.OldFullPath, e.FullPath, wct.ToString());
            }
    
            //  This method is called when the FileSystemWatcher detects an error.
            private static void OnError(object source, ErrorEventArgs e)
            {
                //  Show that an error has been detected.
                Console.WriteLine("The FileSystemWatcher has detected an error");
                //  Give more information if the error is due to an internal buffer overflow.
                if (e.GetException().GetType() == typeof(InternalBufferOverflowException))
                {
                    //  This can happen if Windows is reporting many file system events quickly 
                    //  and internal buffer of the  FileSystemWatcher is not large enough to handle this
                    //  rate of events. The InternalBufferOverflowException error informs the application
                    //  that some of the file system events are being lost.
                    Console.WriteLine(("The file system watcher experienced an internal buffer overflow: " + e.GetException().Message));
                }
            }
    
        }
    }

    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处

    2012年8月15日 上午 04:52
  • 那你為啥不用標準的開啟舊檔對話盒?

    OpenFileDialog


    論壇是網友平等互助 保證解答請至 微軟技術支援服務


    提問時,錯誤情境描述與錯誤訊息很重要,情境描述包含你做了什麼,預期的結果與實際發生的結果。一個最爛的問法範例:「我的電腦電腦怎麼不能開機?」誰知道你家是不是沒電還是你根本找不到電源鈕。

    2012年8月15日 下午 12:24