C# 解决 PathTooLongException 重写了整个System.IO

最近,因为公司的档案管理系统需要支持长文件名,也就是260字符上限的问题,上网找了相关的资料,并且找到了外国一个牛人写的dll,在原有作者的基础上加了我们需要实现的一些方法。所加方法如下:

如大家需要取dll,请留邮箱,或之后我上传到csdn.

public static DirectorySecurity GetDirectorySecurity(string longname)
        {
            int length = 0;
            byte[] buffer;// = new byte[65536];
            bool ret = Win32Interop.GetFileSecurity(longname, (int)(SECURITY_INFORMATION.OWNER_SECURITY_INFORMATION | SECURITY_INFORMATION.GROUP_SECURITY_INFORMATION ), null, 0, out length);
            if (length > 0)
            {
                buffer = new byte[length];
                ret = Win32Interop.GetFileSecurity(longname, (int)(SECURITY_INFORMATION.OWNER_SECURITY_INFORMATION | SECURITY_INFORMATION.GROUP_SECURITY_INFORMATION), buffer, (uint)length, out length);
                DirectorySecurity pSD = new DirectorySecurity();
                pSD.SetSecurityDescriptorBinaryForm(buffer);
                return pSD;
            }
            else
            {
                return null;
            }
        }

        public static DirectorySecurity GetDirectorySecurity(string longname, AccessControlSections sec)
        {
            int length = 0;
            byte[] buffer;// = new byte[65536];
            bool ret = Win32Interop.GetFileSecurity(longname, (int)sec, null, 0, out length);
            if (length > 0)
            {
                buffer = new byte[length];
                ret = Win32Interop.GetFileSecurity(longname, (int)sec, buffer, (uint)length, out length);
                DirectorySecurity pSD = new DirectorySecurity();
                pSD.SetSecurityDescriptorBinaryForm(buffer);
                return pSD;
            }
            else
            {
                return null;
            }
        }

        public static FileSecurity GetFileSecurity(string longname)
        {
            int length = 0;
            byte[] buffer;// = new byte[65536];
            bool ret = Win32Interop.GetFileSecurity(longname, (int)(SECURITY_INFORMATION.OWNER_SECURITY_INFORMATION | SECURITY_INFORMATION.GROUP_SECURITY_INFORMATION), null, 0, out length);
            if (length > 0)
            {
                buffer = new byte[length];
                ret = Win32Interop.GetFileSecurity(longname, (int)(SECURITY_INFORMATION.OWNER_SECURITY_INFORMATION | SECURITY_INFORMATION.GROUP_SECURITY_INFORMATION), buffer, (uint)length, out length);
                FileSecurity pSD = new FileSecurity();
                pSD.SetSecurityDescriptorBinaryForm(buffer);
                return pSD;
            }
            else
            {
                return null;
            }
        }

        public static FileSecurity GetFileSecurity(string longname,AccessControlSections sec)
        {
            int length = 0;
            byte[] buffer;// = new byte[65536];
            bool ret = Win32Interop.GetFileSecurity(longname, (int)sec, null, 0, out length);
            if (length > 0)
            {
                buffer = new byte[length];
                ret = Win32Interop.GetFileSecurity(longname, (int)sec, buffer, (uint)length, out length);
                FileSecurity pSD = new FileSecurity();
                pSD.SetSecurityDescriptorBinaryForm(buffer);
                return pSD;
            }
            else
            {
                return null;
            }
        }


 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 15
    评论
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值