c#完成了先判断文件夹的可读和可写属性然后再拷贝文件的功能

来源:互联网 发布:dnf辅助网站源码 编辑:程序博客网 时间:2024/05/29 17:24

因为对操作系统层面的函数还不是很了解,所以在判断的时候的方式有点挫,但是还是把功能圆满的完成了,希望能对网友们有所帮助。

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Security.AccessControl;


namespace CopyFileByCMD
{
    class FileCopy
    {
        /// <summary>
        /// 判断用户对文件夹是否有写入权限
        /// </summary>
        /// <param name="destFolder"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool IfFolderCanWrite(string destFolder, out string message)
        {
            bool result = false;
            bool canRead = true;
            bool canWrite = false;
            message = "文件夹不可以被写入!";
            DirectorySecurity s = new DirectorySecurity(destFolder, AccessControlSections.Access);
             if(!Directory.Exists(destFolder))
                {
                    message="目标文件夹不存在";
                    MessageBox.Show(message);
                    return result; 
                }
              else
             {
                 //先判断当前用户对文件夹是否有可读权限
                 try
                 {
                     Directory.GetFiles(destFolder);
                 }
                 catch (Exception ex)
                 {
                     //没有可读权限
                     canRead = false;
                 }
                 finally
                 {
                     
                     AuthorizationRuleCollection rules = s.GetAccessRules(true, false, typeof(System.Security.Principal.NTAccount));
                    //当权限都有的时候则count为0(测试得到的结果,暂时没能理解,觉得很挫)
                     if (rules.Count == 0)
                     {
                         result = true;
                     }
                     foreach (FileSystemAccessRule rule in rules)
                     {
                       
                         //当只有canRead而没有canWrite,用户实际上是只有canWrite没有canRead(结果是反的...)                        
                         if (!rule.FileSystemRights.HasFlag(FileSystemRights.WriteData) && rule.FileSystemRights.HasFlag(FileSystemRights.ReadData))
                         {
                                 result = true;
                                 message = "文件夹可以被写入";


                         }
                         // 当canRead和canWrite都有的时候
                         else if (rule.FileSystemRights.HasFlag(FileSystemRights.WriteData) && rule.FileSystemRights.HasFlag(FileSystemRights.ReadData))
                         {
                             //当canRead为true说明是用户实际上两种权限都有则判断出有canWrite权限
                             if (canRead)
                             {
                                 result = true;
                             }
                         }
                     }
                 }
             }
                      
            return result;
        }
        public bool copyFile(string sourceFile, string destFolder)
        {
            bool result = true;
            string message;
            //判断文件夹是否可以被写入
            if (IfFolderCanWrite(destFolder, out message))
            {
                string path = "copy " + sourceFile + " " + destFolder;
                System.Diagnostics.Process p = new System.Diagnostics.Process();
                p.StartInfo.FileName = "cmd.exe ";
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.CreateNoWindow = true;


                p.Start();
                p.StandardInput.WriteLine(path);
                p.StandardInput.WriteLine("exit ");
                p.StandardOutput.ReadToEnd();
                p.Close();
            }
            else
            {
                result = false;
            }
            return result;


        }
    }
}


0 0
原创粉丝点击