none
C# File permissions question

    Question

  • While creating a file synchronization program in C# I tried to make a method 'copy' from LocalFileItem class that uses sing System.IO.File.Copy(destination.Path, Path, true) where Path is 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:\Test is owned by myself(the current user). Does anybody knows what is going on, or how to get around this?

    Here is the code:

    1using System; 
    2using System.Collections.Generic; 
    3using System.Text; 
    4using System.IO; 
    5 
    6 
    7namespace Diones.Util.IO 
    8
    9    /// <summary> 
    10    /// An object representation of a file or directory. 
    11    /// </summary> 
    12    public abstract class FileItem : IComparable 
    13 
    14    { 
    15        protected String path; 
    16        public String Path 
    17        { 
    18            set { this.path = value; } 
    19            get { return this.path; } 
    20        } 
    21        protected bool isDirectory; 
    22        public bool IsDirectory 
    23        { 
    24            set { this.isDirectory = value; } 
    25            get { return this.isDirectory; } 
    26        } 
    27        /// <summary> 
    28        ///  Delete this fileItem. 
    29        /// </summary> 
    30        public abstract void delete(); 
    31        /// <summary> 
    32        ///  Delete this directory and all of its elements. 
    33        /// </summary> 
    34        protected abstract void deleteRecursive(); 
    35        /// <summary> 
    36        ///  Copy this fileItem to the destination directory. 
    37        /// </summary> 
    38        public abstract void copy(FileItem fileD); 
    39        /// <summary> 
    40        ///  Copy this directory and all of its elements 
    41        /// to the destination directory. 
    42        /// </summary> 
    43        protected abstract void copyRecursive(FileItem fileD); 
    44        /// <summary> 
    45        /// Creates a FileItem from a string path. 
    46        /// </summary> 
    47        /// <param name="path"></param> 
    48        public FileItem(String path) 
    49        { 
    50            Path = path; 
    51            if (path.EndsWith("\\") || path.EndsWith("/")) IsDirectory = true
    52            else IsDirectory = false
    53        } 
    54        /// <summary> 
    55        /// Creates a FileItem from a FileSource directory. 
    56        /// </summary> 
    57        /// <param name="directory"></param> 
    58        public FileItem(FileSource directory) 
    59        { 
    60            Path = directory.Path; 
    61        } 
    62        public override String ToString() 
    63        { 
    64            return Path; 
    65        } 
    66        public abstract int CompareTo(object b); 
    67    } 
    68    /// <summary> 
    69    /// A file or directory on the hard disk 
    70    /// </summary> 
    71    public class LocalFileItem : FileItem 
    72    { 
    73        public override void delete() 
    74        { 
    75            if (!IsDirectory) File.Delete(this.Path); 
    76            else deleteRecursive(); 
    77        } 
    78        protected override void deleteRecursive() 
    79        { 
    80            Directory.Delete(Path, true); 
    81        } 
    82        public override void copy(FileItem destination) 
    83        { 
    84            if (!IsDirectory) File.Copy(destination.Path, Path, true); 
    85            else copyRecursive(destination); 
    86        } 
    87        protected override void copyRecursive(FileItem destination) 
    88        { 
    89            Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory( 
    90                Path, destination.Path, true); 
    91        } 
    92        /// <summary> 
    93        /// Create's a LocalFileItem from a string path 
    94        /// </summary> 
    95        /// <param name="path"></param> 
    96        public LocalFileItem(String path) 
    97            : base(path) 
    98        { 
    99        } 
    100        /// <summary> 
    101        /// Creates a LocalFileItem from a FileSource path 
    102        /// </summary> 
    103        /// <param name="path"></param> 
    104        public LocalFileItem(FileSource path) 
    105            : base(path) 
    106        { 
    107        } 
    108        public override int CompareTo(object obj) 
    109        { 
    110            if (obj is FileItem) 
    111            { 
    112                FileItem fi = (FileItem)obj; 
    113                if (File.GetCreationTime(this.Path).CompareTo 
    114                    (File.GetCreationTime(fi.Path)) > 0) return 1; 
    115                else if (File.GetCreationTime(this.Path).CompareTo 
    116                    (File.GetCreationTime(fi.Path)) < 0) return -1; 
    117                else 
    118                { 
    119                    if (File.GetLastWriteTime(this.Path).CompareTo 
    120                        (File.GetLastWriteTime(fi.Path)) < 0) return -1; 
    121                    else if (File.GetLastWriteTime(this.Path).CompareTo 
    122                        (File.GetLastWriteTime(fi.Path)) > 0) return 1; 
    123                    else return 0; 
    124                } 
    125            } 
    126            else 
    127                throw new ArgumentException("obj isn't a FileItem"); 
    128        } 
    129    } 
    130
    131 


    This signature cannot be displayed because it violates Microsoft's intellectual property.
    Saturday, August 23, 2008 2:53 PM

Answers

  • This is the code:

            public static void setAccessRule ( string directory )  
            {  
                System.Security.AccessControl.DirectorySecurity sec =  
                                        System.IO.Directory.GetAccessControl ( directory );  
                FileSystemAccessRule accRule = new FileSystemAccessRule ( Globals.userIdentity,  
                     FileSystemRights.FullControl, AccessControlType.Allow );  
                sec.AddAccessRule ( accRule );  
            }                                               // setACL  
     

    UserIdentity is hostName\YourSignInName.  hostName is also machineName(server)

    Neeless to say you must run your app as administrator if you are in Vista.

    On a side note, could you please refrain from posting code with line numbers. It is an additional effort to get rid of them. I had to copy it to my VS2008 to figure out some details. It is not a reprimand:) The line numbers are not needed.

    It is a useful chunk of code for me too. I archived it with appreciation.  Thanks.


    AlexB
    Saturday, August 23, 2008 4:18 PM