c 讀取LOG檔案並解決讀取提示被其他程序占用問題

2022-06-28 13:15:11 字數 1150 閱讀 5106

c# 讀寫檔案時檔案正由另一程序使用,因此該程序無法訪問該檔案,在io處理上遇到了無法操作的問題。

檔案「c:\u_ex.log」正由另一程序使用,因此該程序無法訪問該檔案。

u_ex.log是乙個日誌檔案,不定時都可能由另外的程式對它進行日誌記錄寫入操作。

當日誌檔案被其他程式所占用的情況下,用下面三種方式讀取情況!

string url = @"c:\inetpub\logs\logfiles\w3svc1\u_ex.log";

第一種:

filestream fs = file.openread(url);

streamreader sr = new streamreader((system.io.stream)fs, system.text.encoding.default);

第二種:

streamreader sr = file.opentext(url);

第三種:

filestream fs = new filestream(url, filemode.open, fileaccess.read, fileshare.readwrite);

streamreader sr = new streamreader(fs, system.text.encoding.default);

正確讀取。

這樣的情況,不單要與唯讀方式開啟log檔案,而且,需要共享鎖。還必須要選擇flieshare方式為readwrite。因為隨時有其他程式對其進行寫操作

詳細**:

string content;

string url = @"c:\inetpub\logs\logfiles\w3svc1\u_ex17020815.log";

using (filestream fs = new filestream(url, filemode.open, fileaccess.read, fileshare.readwrite))

}//如果content陣列值的數量為0說明讀取的檔案沒有資料

if (content.count() != 0)

}}

讀取配置檔案並讀取檔案

配置檔案 url users z ten documents name myfile.txt url是讀寫檔案的檔案位址,name是讀寫檔案的檔名稱 fileinputstream is null try catch filenotfoundexception e2 properties pt ne...

開啟並讀取檔案

開啟並讀取檔案 file open r c users administrator desktop walden.txt r lines file.readlines words for line in lines tmp list line.split for word in tmp list 對...

解決C 讀取檔案亂碼問題

儲存如下內容到mapconfig.json檔案 讀取時出現亂碼 其中,讀取部分的 如下 jobject jmapconfig jobject.parse system.io.file.readalltext spath jarray aarray jmapconfig basemap as jarr...