C#文件操作集合八(文件操作权限)

前言

        文件操作是开发过程中经常遇到的,如何创建文件、处理文件、保存文件以及读取文件这些工作是每一位开发都必须经历的。

这里围绕C#文件操作内容,我做了一次系统的梳理,主要包括以下几个模块:

1、文件内容操作:文件读写相关

2、文件检测

3、文件及目录创建和删除

4、文件移动

5、文件加密、解密

6、文件上传、下载

7、文件压缩相关

8、文件操作权限相关


一、小节摘要

本节主要讲文件操作权限相关内容,主要包括一下方面:

1、文件是否存在或无权访问 FileIsExist(string path)

2、文件是否只读 FileIsReadOnly(string fullpath)

3、设置文件是否只读   SetFileReadonly(string fullpath, bool flag)

4、取文件名    GetFileName(string fullpath, bool removeExt)

5、取文件创建时间   GetFileCreateTime(string fullpath)

6、取文件最后存储时间  GetLastWriteTime(string fullpath)

7、获取指定文件详细属性   GetFileAttibe(string filePath)

二、详细内容

1、文件是否存在或无权访问 FileIsExist(string path)

public static bool FileIsExist(string path)
{
    return File.Exists(path);
}

2、文件是否只读 FileIsReadOnly(string fullpath)

public static bool FileIsReadOnly(string fullpath)
{
    FileInfo file = new FileInfo(fullpath);
    if ((file.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
    {
        return true;
    }
    else
    {
        return false;
    }
}

3、设置文件是否只读   SetFileReadonly(string fullpath, bool flag)

public static void SetFileReadonly(string fullpath, bool flag)
{
    FileInfo file = new FileInfo(fullpath);

    if (flag)
    {
        // 添加只读属性
        file.Attributes |= FileAttributes.ReadOnly;
    }
    else
    {
        // 移除只读属性
        file.Attributes &= ~FileAttributes.ReadOnly;
    }
}

4、取文件名    GetFileName(string fullpath, bool removeExt)

public static string GetFileName(string fullpath, bool removeExt)
{
    FileInfo fi = new FileInfo(fullpath);
    string name = fi.Name;
    if (removeExt)
    {
        name = name.Remove(name.IndexOf('.'));
    }
    return name;
}

5、取文件创建时间   GetFileCreateTime(string fullpath)

public static DateTime GetFileCreateTime(string fullpath)
{
    FileInfo fi = new FileInfo(fullpath);
    return fi.CreationTime;
}

6、取文件最后存储时间  GetLastWriteTime(string fullpath)

public static DateTime GetLastWriteTime(string fullpath)
{
    FileInfo fi = new FileInfo(fullpath);
    return fi.LastWriteTime;
}

7、获取指定文件详细属性   GetFileAttibe(string filePath)

public static string GetFileAttibe(string filePath)
{
    string str = "";
    FileInfo objFI = new FileInfo(filePath);
    str += "详细路径:" + objFI.FullName 
        + "<br>文件名称:" + objFI.Name 
        + "<br>文件长度:" + objFI.Length.ToString()
        + "字节<br>创建时间" + objFI.CreationTime.ToString()
        + "<br>最后访问时间:" + objFI.LastAccessTime.ToString() 
        + "<br>修改时间:" + objFI.LastWriteTime.ToString() 
        + "<br>所在目录:" + objFI.DirectoryName 
        + "<br>扩展名:" + objFI.Extension;
    return str;
}

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MarcoPro

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值