LOGO OA教程 ERP教程 模切知識(shí)交流 PMS教程 CRM教程 開(kāi)發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

C# 文件的隱藏、只讀、占用判斷介紹

admin
2025年7月20日 21:52 本文熱度 537

       在C#文件操作中,文件的隱藏屬性、只讀屬性以及文件占用狀態(tài)都是非常重要的概念,它們直接影響程序?qū)ξ募牟僮髂芰涂煽啃浴?/section>

1. 文件隱藏屬性(Hidden)

重要性:

  • 系統(tǒng)文件或配置文件通常被標(biāo)記為隱藏,防止用戶(hù)誤操作

  • 程序可能需要訪(fǎng)問(wèn)這些隱藏文件來(lái)完成特定功能

  • 用戶(hù)界面程序可能需要考慮是否顯示隱藏文件

2. 文件只讀屬性(ReadOnly)

重要性:

  • 防止重要文件被意外修改

  • 某些系統(tǒng)文件需要保持只讀狀態(tài)以保證系統(tǒng)穩(wěn)定性

  • 程序需要正確處理只讀文件,避免操作失敗

3. 文件占用狀態(tài)

重要性:

  • 文件被占用時(shí)嘗試操作會(huì)導(dǎo)致異常

  • 需要正確處理文件占用情況,提高程序健壯性

  • 多線(xiàn)程/多進(jìn)程環(huán)境下尤為重要

4、在程序中注意事項(xiàng)

(1)操作前檢查屬性:在修改或刪除文件前檢查只讀和隱藏屬性

(2)異常處理:對(duì)文件操作進(jìn)行適當(dāng)?shù)膖ry-catch處理

(3)用戶(hù)提示:當(dāng)遇到只讀或占用文件時(shí),給用戶(hù)明確的提示

(4)權(quán)限考慮:某些操作可能需要管理員權(quán)限

(5)資源釋放:確保及時(shí)釋放文件句柄,避免造成文件占用

以下為文件只讀、隱藏、占用判斷方法:

1、判斷文件是否被占用

/// <summary>/// 判斷文件是否被占用/// 如果文件已經(jīng)被其它程序使用,則打開(kāi)失敗。/// </summary>/// <param name="filePath">文件路徑</param>/// <returns>true:被占用</returns>public bool IsFileInUse(string filePath){    bool inUse = false;    if (!File.Exists(filePath)) return false;    try    {        using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None))        {            inUse = false;            fs.Close();        }    }    catch    {        inUse = true;    }    return inUse;}

2、獲取指定目錄中所有隱藏的文件

/// <summary>/// 獲取指定目錄中所有隱藏的文件/// </summary>/// <param name="FileDir"></param>/// <returns></returns>public List<stringGetHideFile(string FileDir){    List<string> RetList = new List<string>();    string[] haidfile = Directory.GetFiles(FileDir, "*.*", SearchOption.AllDirectories);    foreach (string file in haidfile)    {        FileInfo fi = new FileInfo(file);        if ((fi.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)        {            RetList.Add(fi.FullName);        }    }    return RetList;}

3、獲取指定目錄中所有的只讀文件

/// <summary>/// 獲取指定目錄中所有的只讀文件/// </summary>/// <param name="FileDir"></param>/// <returns></returns>public List<stringGetReadOnly(string FileDir){    List<string> RetList = new List<string>();    string[] haidfile = Directory.GetFiles(FileDir, "*.*", SearchOption.AllDirectories);    foreach (string file in haidfile)    {        FileInfo fi = new FileInfo(file);        if ((fi.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)        {            RetList.Add(fi.FullName);        }    }    return RetList;}

4、創(chuàng)建隱藏文件夾

/// <summary>/// 創(chuàng)建隱藏文件夾/// </summary>/// <param name="path">文件路徑</param>public void CreateHiddenDires(string path){    DirectoryInfo di = new DirectoryInfo(path);    DateTime times = DateTime.Now;    if (!di.Exists)    {        di.Create();        di.LastWriteTime = times;        di.LastAccessTime = times;        SetDireHidden(path);    }}

5、設(shè)置文件夾隱藏

/// <summary>/// 設(shè)置文件夾隱藏/// </summary>/// <param name="path">文件路徑</param>public void SetDireHidden(string path){    DirectoryInfo dir = new DirectoryInfo(path);    File.SetAttributes(path, dir.Attributes | FileAttributes.Hidden & ~FileAttributes.ReadOnly);}

6、取消文件夾的隱藏只讀屬性

/// <summary> 取消文件夾的隱藏只讀屬性 </summary>/// <param name="path">文件名(包含路徑)</param>public void roHidDirNudo(string path){    DirectoryInfo di = new DirectoryInfo(path);    if (di.Exists) { File.SetAttributes(path, di.Attributes & ~FileAttributes.ReadOnly & ~FileAttributes.Hidden); }}

7、設(shè)置文件只讀+隱藏

/// <summary> /// 設(shè)置文件只讀+隱藏/// </summary>/// <param name="path">文件名(包含路徑)</param>public void SetFileHiddenReadOnly(string path){    FileInfo fi = new FileInfo(path);    File.SetAttributes(path, fi.Attributes | FileAttributes.Hidden | FileAttributes.ReadOnly);}

8、設(shè)置文件只讀

/// <summary> /// 設(shè)置文件只讀/// </summary>/// <param name="path">文件名(包含路徑)</param>public void SetFileReadOnly(string path){    FileInfo fi = new FileInfo(path);    File.SetAttributes(path, fi.Attributes | ~FileAttributes.Hidden | FileAttributes.ReadOnly);}

9、設(shè)置文件隱藏

/// <summary> /// 設(shè)置文件隱藏/// </summary>/// <param name="path">文件名(包含路徑)</param>public void SetFileHidden(string path){    FileInfo fi = new FileInfo(path);    File.SetAttributes(path, fi.Attributes | FileAttributes.Hidden | ~FileAttributes.ReadOnly);}

10、取消文件的只讀、隱藏屬性

/// <summary> 取消文件的只讀、隱藏屬性 </summary>/// <param name="path">文件名(包含路徑)</param>public void CancelFileHiddenReadOnly(string path){    FileInfo fi = new FileInfo(path);    if (fi.Exists)    {        File.SetAttributes(path, fi.Attributes & ~FileAttributes.Hidden & ~FileAttributes.ReadOnly);    }}


閱讀原文:原文鏈接


該文章在 2025/7/21 10:28:29 編輯過(guò)
關(guān)鍵字查詢(xún)
相關(guān)文章
正在查詢(xún)...
點(diǎn)晴ERP是一款針對(duì)中小制造業(yè)的專(zhuān)業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國(guó)內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對(duì)港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場(chǎng)、車(chē)隊(duì)、財(cái)務(wù)費(fèi)用、相關(guān)報(bào)表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場(chǎng)作業(yè)而開(kāi)發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類(lèi)企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉(cāng)儲(chǔ)管理系統(tǒng)提供了貨物產(chǎn)品管理,銷(xiāo)售管理,采購(gòu)管理,倉(cāng)儲(chǔ)管理,倉(cāng)庫(kù)管理,保質(zhì)期管理,貨位管理,庫(kù)位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號(hào)管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時(shí)間、不限用戶(hù)的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved

黄频国产免费高清视频,久久不卡精品中文字幕一区,激情五月天AV电影在线观看,欧美国产韩国日本一区二区
少妇把腿扒开让我添免费视频 | 亚洲一本之道高清在线观看 | 亚洲精品国产精品乱码不卡 | 亚洲欧美高清在线精品二区 | 亚洲系列一区A久久 | 亚州三级久久电影 |