关于 C# 中的文件权限

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-01 09:13:21  来源:igfitidea点击:

About File permissions in C#

提问by Diones

While creating a file synchronization program in C# I tried to make a method copyin LocalFileItemclass that uses System.IO.File.Copy(destination.Path, Path, true)method where Pathis a string.
After executing this code with destination. Path = "C:\\Test2"and this.Path = "C:\\Test\\F1.txt"I get an exception saying that I do not have the required file permissions to do this operation on C:\Test, but C:\Testis owned by myself (the current user).
Does anybody knows what is going on, or how to get around this?

在 C# 中创建文件同步程序时,我尝试copyLocalFileItem类中创建一个使用System.IO.File.Copy(destination.Path, Path, true)方法 where Pathis a 的方法string
使用目的地执行此代码后。Path = "C:\\Test2"this.Path = "C:\\Test\\F1.txt"我得到一个异常说我不具备所需的文件权限做此操作C:\测试,但C:\测试是通过自己拥有的(当前用户)
有谁知道发生了什么,或者如何解决这个问题?

Here is the original code complete.

这里是完整的原始代码。

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Diones.Util.IO
{
    /// <summary>
    /// An object representation of a file or directory.
    /// </summary>
    public abstract class FileItem : IComparable

    {
        protected String path;
        public String Path
        {
            set { this.path = value; }
            get { return this.path; }
        }
        protected bool isDirectory;
        public bool IsDirectory
        {
            set { this.isDirectory = value; }
            get { return this.isDirectory; }
        }
        /// <summary>
        ///  Delete this fileItem.
        /// </summary>
        public abstract void delete();
        /// <summary>
        ///  Delete this directory and all of its elements.
        /// </summary>
        protected abstract void deleteRecursive();
        /// <summary>
        ///  Copy this fileItem to the destination directory.
        /// </summary>
        public abstract void copy(FileItem fileD);
        /// <summary>
        ///  Copy this directory and all of its elements
        /// to the destination directory.
        /// </summary>
        protected abstract void copyRecursive(FileItem fileD);
        /// <summary>
        /// Creates a FileItem from a string path.
        /// </summary>
        /// <param name="path"></param>
        public FileItem(String path)
        {
            Path = path;
            if (path.EndsWith("\") || path.EndsWith("/")) IsDirectory = true;
            else IsDirectory = false;
        }
        /// <summary>
        /// Creates a FileItem from a FileSource directory.
        /// </summary>
        /// <param name="directory"></param>
        public FileItem(FileSource directory)
        {
            Path = directory.Path;
        }
        public override String ToString()
        {
            return Path;
        }
        public abstract int CompareTo(object b);
    }
    /// <summary>
    /// A file or directory on the hard disk
    /// </summary>
    public class LocalFileItem : FileItem
    {
        public override void delete()
        {
            if (!IsDirectory) File.Delete(this.Path);
            else deleteRecursive();
        }
        protected override void deleteRecursive()
        {
            Directory.Delete(Path, true);
        }
        public override void copy(FileItem destination)
        {
            if (!IsDirectory) File.Copy(destination.Path, Path, true);
            else copyRecursive(destination);
        }
        protected override void copyRecursive(FileItem destination)
        {
            Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory(
                Path, destination.Path, true);
        }
        /// <summary>
        /// Create's a LocalFileItem from a string path
        /// </summary>
        /// <param name="path"></param>
        public LocalFileItem(String path)
            : base(path)
        {
        }
        /// <summary>
        /// Creates a LocalFileItem from a FileSource path
        /// </summary>
        /// <param name="path"></param>
        public LocalFileItem(FileSource path)
            : base(path)
        {
        }
        public override int CompareTo(object obj)
        {
            if (obj is FileItem)
            {
                FileItem fi = (FileItem)obj;
                if (File.GetCreationTime(this.Path).CompareTo
                    (File.GetCreationTime(fi.Path)) > 0) return 1;
                else if (File.GetCreationTime(this.Path).CompareTo
                    (File.GetCreationTime(fi.Path)) < 0) return -1;
                else
                {
                    if (File.GetLastWriteTime(this.Path).CompareTo
                        (File.GetLastWriteTime(fi.Path)) < 0) return -1;
                    else if (File.GetLastWriteTime(this.Path).CompareTo
                        (File.GetLastWriteTime(fi.Path)) > 0) return 1;
                    else return 0;
                }
            }
            else
                throw new ArgumentException("obj isn't a FileItem");
        }
    }
}

采纳答案by Micha? Piaskowski

It seems you have misplaced the parameters in File.Copy(), it should be File.Copy(string source, string destination).

看来您在 File.Copy() 中放错了参数,它应该是 File.Copy(string source, string destination)。

Also is "C:\Test2" a directory? You can't copy file to a directory. Use something like that instead:

“C:\Test2”也是一个目录吗?您无法将文件复制到目录。改用类似的东西:

File.Copy( 
    sourceFile,
    Path.Combine(destinationDir,Path.GetFileName(sourceFile))
    )
;

回答by Rob Cooper

I'm kinda guessing here, but could it be because:

我在这里有点猜测,但可能是因为:

  • You are trying to perform file operations in C: root? (there may be protection on this by Vista if you are using it - not sure?)
  • You are trying to copy to a non-existant directory?
  • The file already exists and may be locked? (i.e you have not closed another application instance)?
  • 您正在尝试在 C: root 中执行文件操作?(如果您正在使用它,Vista 可能会对此提供保护 - 不确定?)
  • 您正在尝试复制到一个不存在的目录?
  • 该文件已经存在并且可能被锁定?(即您还没有关闭另一个应用程序实例)?

Sorry I cant be of more help, I have rarely experienced problems with File.Copy.

抱歉,我无法提供更多帮助,我很少遇到 File.Copy 问题。

回答by Diones

I was able to solve the problem, Michal pointed me to the right direction. The problem was that I tried to use File.Copy to copy a file from one location to another, while the Copy method does only copy all the contents from one file to another(creating the destination file if it does not already exists). The solution was to append the file name to the destination directory. Thanks for all the help!

我能够解决问题,Michal 为我指明了正确的方向。问题是我尝试使用 File.Copy 将文件从一个位置复制到另一个位置,而 Copy 方法仅将所有内容从一个文件复制到另一个文件(如果目标文件尚不存在,则创建该文件)。解决方案是将文件名附加到目标目录。感谢所有的帮助!