在C#文件操作中,文件的隐藏属性、只读属性以及文件占用状态都是非常重要的概念,它们直接影响程序对文件的操作能力和可靠性。1. 文件隐藏属性(Hidden)
重要性:
2. 文件只读属性(ReadOnly)
重要性:
- 防止重要文件被意外修改 
- 某些系统文件需要保持只读状态以保证系统稳定性 
- 程序需要正确处理只读文件,避免操作失败 
3. 文件占用状态
重要性:
- 文件被占用时尝试操作会导致异常 
- 需要正确处理文件占用情况,提高程序健壮性 
- 多线程/多进程环境下尤为重要 
4、在程序中注意事项
(1)操作前检查属性:在修改或删除文件前检查只读和隐藏属性
(2)异常处理:对文件操作进行适当的try-catch处理
(3)用户提示:当遇到只读或占用文件时,给用户明确的提示
(4)权限考虑:某些操作可能需要管理员权限
(5)资源释放:确保及时释放文件句柄,避免造成文件占用
1、判断文件是否被占用
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、获取指定目录中所有隐藏的文件
public List<string> GetHideFile(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、获取指定目录中所有的只读文件
public List<string> GetReadOnly(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、创建隐藏文件夹
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、设置文件夹隐藏
public void SetDireHidden(string path){    DirectoryInfo dir = new DirectoryInfo(path);    File.SetAttributes(path, dir.Attributes | FileAttributes.Hidden & ~FileAttributes.ReadOnly);}
6、取消文件夹的隐藏只读属性
public void roHidDirNudo(string path){    DirectoryInfo di = new DirectoryInfo(path);    if (di.Exists) { File.SetAttributes(path, di.Attributes & ~FileAttributes.ReadOnly & ~FileAttributes.Hidden); }}
7、设置文件只读+隐藏
public void SetFileHiddenReadOnly(string path){    FileInfo fi = new FileInfo(path);    File.SetAttributes(path, fi.Attributes | FileAttributes.Hidden | FileAttributes.ReadOnly);}
8、设置文件只读
public void SetFileReadOnly(string path){    FileInfo fi = new FileInfo(path);    File.SetAttributes(path, fi.Attributes | ~FileAttributes.Hidden | FileAttributes.ReadOnly);}
9、设置文件隐藏
public void SetFileHidden(string path){    FileInfo fi = new FileInfo(path);    File.SetAttributes(path, fi.Attributes | FileAttributes.Hidden | ~FileAttributes.ReadOnly);}
10、取消文件的只读、隐藏属性
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 编辑过