Karl
Karl

Reputation: 5822

System.Security.Permission.FileIOPermission

Why do I get a "System.Security.Permission.FileIOPermission" error? Below is basically my whole program. It is extremely simple. All it needs to do is create a copy of a folder. It works on my PC and and number of my colleagues' PCs, but for some it gives the error above.

Any ideas?

        public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
        {
            // Check if the target directory exists, if not, create it.
            if (Directory.Exists(target.FullName) == false)
            {
                Directory.CreateDirectory(target.FullName);
            }

            // Copy each file into it’s new directory.
            foreach (FileInfo fi in source.GetFiles())
            {
                Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name);
                fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
            }

            // Copy each subdirectory using recursion.
            foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
            {
                DirectoryInfo nextTargetSubDir =
                target.CreateSubdirectory(diSourceSubDir.Name);
                CopyAll(diSourceSubDir, nextTargetSubDir);
            }
        }

Upvotes: 1

Views: 4761

Answers (3)

Karl
Karl

Reputation: 5822

Installing .net 3.5 fixes the problem (changes the default FileIOPermissions on the local machine to grant permission to applications run over the network)

Upvotes: 1

PVitt
PVitt

Reputation: 11770

The problem will be insufficent rights. The operating system denies access to a directory, because the user who runs the program is not allowed to create directories or copy files in that particular directory.

Upvotes: 1

TheVillageIdiot
TheVillageIdiot

Reputation: 40527

On the systems where this is happening the user logged in might not have read or write permissions (most probably read, because if you can create target directory you can write to it). More information about FileIOPermission can be found here at msdn.

Upvotes: 1

Related Questions