C#文件安全管理解析

本文详细介绍了C#中关于文件的安全管理,包括文件和目录的常用操作,如读写、附加;文件权限管理,如GetAccessControl和SetAccessControl方法;文件的彻底删除及其多轮粉碎策略;以及文件的加密解密操作Encrypt和Decrypt。这些内容对于提升项目中的文件安全性至关重要。
摘要由CSDN通过智能技术生成

在实际的项目开发中,我们经常需要使用到文件的I/O操作,主要包含对文件的增改删查等操作,这些基本的操作我们都是很熟悉,但是较少的人去考虑文件的安全和操作的管理等方面,例如文件的访问权限管理,文件数据的彻底删除和数据的恢复等等,这一系列的操作需要我们对.NET的相关知识有一个深刻的学习。

在本文章主要介绍文件和目录的一些基本操作,以及文件目录的权限和安全设置的相关内容。

一.DotNet文件目录常用操作:

提到文件的I/O操作,这个对于每一个开发者来说都不是陌生的事,因为这些操作是我们在项目开发过程中经常使用到的一些操作。那么在.NET中操作文件的类在System.IO命名空间下,一下介绍一下常见的I/O操作类:

DiveInfo:提供了对逻辑磁盘的基本信息访问的途径。(只能查看信息,不能做任何修改。)

System.Environment:用来枚举驱动器。(不能获取驱动器的属性)

System.Management:.NET针对WMI调用。

Directory和DircetoryInfo:用于操作目录。(前者为静态类,后者则须在实例化后调用,功能上相同)

File和FileInfo:用于操作文件。(前者为静态类,后者须实例化后调用,功能上相同)

以上介绍了一些文件的基本操作类,本次主要讲解目录和文件操作,一下给出文件和目录操作的一些基本方法:

1.文件常规操作:
(1).文件读写操作:
   /// <summary>
        /// 写文件
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="content">文件内容</param>
        /// <param name="encoding">指定文件编码</param>
        protected void Write_Txt(string fileName, string content, string encoding)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentNullException(fileName);
            }
            if (string.IsNullOrEmpty(content))
            {
                throw new ArgumentNullException(content);
            }
            if (string.IsNullOrEmpty(encoding))
            {
                throw new ArgumentNullException(encoding);
            }
            var code = Encoding.GetEncoding(encoding);
            var htmlfilename = HttpContext.Current.Server.MapPath("Precious\\" + fileName + ".txt");
            var str = content;
            var sw = StreamWriter.Null;
            try
            {
                using (sw = new StreamWriter(htmlfilename, false, code))
                {
                    sw.Write(str);
                    sw.Flush();
                }
            }
            catch (IOException ioex)
            {
                throw new IOException(ioex.Message);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                sw.Close();
            }
        }
 
        /// <summary>
        /// 读文件
        /// </summary>
        /// <param name="filename">文件路径</param>
        /// <param name="encoding">文件编码</param>
        /// <returns></returns>
        protected string Read_Txt(string filename, string encoding)
        {
            if (string.IsNullOrEmpty(filename))
            {
                throw new ArgumentNullException(filename);
            }
            if (string.IsNullOrEmpty(encoding))
            {
                throw new ArgumentNullException(encoding);
            }
            var code = Encoding.GetEncoding(encoding);
            var temp = HttpContext.Current.Server.MapPath("Precious\\" + filename + ".txt");
            var str = string.Empty;
            if (!System.IO.File.Exists(temp)) return str;
            var sr = StreamReader.Null;
            try
            {
                using (sr = new StreamReader(temp, code))
                {
                    str = sr.ReadToEnd();
                }
            }
            catch (IOException ioex)
            {
                throw new IOException(ioex.Message);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                sr.Close();
            }
            return str;
        }
(2).文件附加操作:
    /// <summary>
        /// 拷贝文件
        /// </summary>
        /// <param name="orignFile">原始文件</param>
        /// <param name="newFile">新文件路径</param>
        public static void FileCoppy(string orignFile, string newFile)
        {
            if (string.IsNullOrEmpty(orignFile))
            {
                throw new ArgumentException(orignFile);
            }
            if (string.IsNullOrEmpty(newFile))
            {
                throw new ArgumentException(newFile);
            }
            System.IO.File.Copy(orignFile, newFile, true);
        }
 
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="path">路径</param>
        public static void FileDel(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentException(path);
            }
            System.IO.File.Delete(path);
        }
 
        /// <summary>
        /// 移动文件
        /// </summary>
        /// <param name="orignFile">原始路径</param>
        /// <param name="newFile">新路径</param>
        public static void FileMove(string orignFile, string newFile)
        {
            if (string.IsNullOrEmpty(orignFile))
            {
                throw new ArgumentException(orignFile);
            }
            if (string.IsNullOrEmpty(newFile))
            {
                throw new ArgumentException(newFile);
            }
            System.IO.File.Move(orignFile, newFile);
        }
2.目录常规操作:
   /// <summary>
        /// 在当前目录下创建目录
        /// </summary>
        /// <param name="orignFolder">当前目录</param>
        /// <param name="newFloder">新目录</param>
        public static void FolderCreate(string orignFolder, string newFloder)
        {
            if (string.IsNullOrEmpty(orignFolder))
            {
                throw new ArgumentException(orignFolder);
            }
            if (string.IsNullOrEmpty(newFloder))
            {
                throw new ArgumentException(newFloder);
            }
            Directory.SetCurrentDirectory(orignFolder);
            Directory.CreateDirectory(newFloder);
        }
 
        /// <summary>
        /// 创建文件夹
        /// </summary>
        /// <param name="path"></param>
        public static void FolderCreate(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentException(path);
  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

lmr廖

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

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

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

打赏作者

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

抵扣说明:

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

余额充值