0
votes

I can't copy pdf files downloaded from the internet.

I've even set the File Attributes to normal based on a quick search, but that doesn't work.

All the paths and directories have full access and other types of files can be copied.

Code:

using System;
using System.IO;

namespace Test_Console_App
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var permission = new FileIOPermission(FileIOPermissionAccess.Write, @"D:\dummy.pdf"); // Alternatives tried D:\, E:\ E:\dummy.pdf
                var permissionSet = new PermissionSet(PermissionState.None);
                permissionSet.AddPermission(permission);
                if (permissionSet.IsSubsetOf(AppDomain.CurrentDomain.PermissionSet))
                {
                    File.SetAttributes(@"D:\dummy.pdf", FileAttributes.Normal);
                    if (File.Exists(@"E:\dummy.pdf"))
                    {
                        File.Delete(@"E:\dummy.pdf");
                    }

                    File.Copy(@"D:\dummy.pdf", @"E:\dummy.pdf", true);
                    File.SetAttributes(@"E:\dummy.pdf", FileAttributes.Normal);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            Console.ReadKey();
        }
    }
}

Error:

System.UnauthorizedAccessException: Access to the path 'E:\dummy.pdf' is denied.

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)

at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite, Boolean checkHost)

at Test_Console_App.Program.Main(String[] args) in D:\Projects\Test_Console_App\Test_Console_App\Program.cs:line 26

3
Check ACL of "E:".Uwe Keim
Ask your company's IT staff to help you get write access to the E: drive. After checking that E:\dummy.pdf is actually a file and not a directory, that happens a bit too often.Hans Passant
Are you sure you havent got the documents open in another program on your machine or something?mouldycurryness

3 Answers

0
votes

You can check if you have the access of the folder. Is it a drive mapped to a network drive. Try to Right-click on the containing folder and see the Security tab to modify rights.

As per Msdn, exception happens because of:

  1. The caller does not have the required permission.
  2. The file is an executable file that is in use.
  3. Path is a directory.
  4. Path specified a read-only file.
0
votes

According to - File.Copy Method

Exceptions - UnauthorizedAccessException - The caller does not have the required permission.

You could check your rights to the directory you are trying to copy the file into using this answer - How to check Read and write permissions on folder in C#

0
votes

Here you can have access issues on source and destination. Access on file "D:\dummy.pdf" is controlled by ACL. You can check ACL by checking Properties>Security of file. You should have minimum read access.

If file exists then check for ACL for E:\dummy.pdf destination file too. You should and read/write/delete access on this file.

The modified code should be like below:

if(File.Exists(@"E:\dummy.pdf"))
{
    File.Delete(@"E:\dummy.pdf");
}

File.Copy(@"D:\dummy.pdf", @"E:\dummy.pdf");

There are alternate ways to copy file using Backup Read/Write COM APi's in .Net