The File.Copy method makes the file inaccessible

I have been struggling with this for some time. I cannot access the file after calling the File.Copy method. Here is what I tried:

File.Copy(sourceFile, destinationFile, true);
FileStream fs = File.Open(destinationFile, FileMode.Open);

I get a UnauthorizedAccessException in the second line. It says: access to the path ... is denied. I also tried to offer a suggestion here , but that didn't work.

Any help is appreciated.

EDIT: That's what I found out. If I do this:

File.Copy(sourceFile, destinationFile, true);
FileStream fs = File.Open(destinationFile, FileMode.Open, FileAccess.Read);

It does not throw an exception. The file I'm trying to get is read-only. So, I tried to remove the read-only attribute as follows:

File.Copy(sourceFile, destinationFile, true);
FileInfo fileInfo = new FileInfo(destinationFile);
fileInfo.IsReadOnly = false;
FileStream fs = File.Open(destinationFile, FileMode.Open, FileAccess.ReadWrite);

, . , , , . , . Windows , . , , .

+3
2

, , File.Copy, , ? NTFS ?

, . .

Update

http://msdn.microsoft.com/en-us/library/b9skfh7s.aspx - UnauthorizedAccessException, File.Open, " ..." . (edit: File.Copy, )

, File.Copy , . , , , .

- , , File.Open?

+5

!


   private bool CheckFileHasCopied(string FilePath)
    {
        try
        {
            if (File.Exists(FilePath))
            {
                using (File.OpenRead(FilePath))
                {
                    return true;
                }
            }
            else
            {
                return false;
            }
        }
        catch (Exception)
        {
            Thread.Sleep(2000);
            return CheckFileHasCopied(FilePath);
        }

    }

if (CheckFileHasCopied(destinationFile)) { File.Delete(sourceFile); }
-1

All Articles