locked
Can't Copy Resource File To A Directory, Access Is Denied. RRS feed

  • Question

  • Hello, I use Visual Studio 2012 Ultimate With Blend In My Windows 8 Pro 64-Bit. I Have A Problem While Copying Some .xls (Excel 97-2003) files to any path at runtime, The .xls files are in my resources. I use this code for copying my resource files:

    My.Computer.FileSystem.WriteAllBytes("Folder Path", My.Resources._1, False)

    the error I get is Access is denied, I have full administrative privileges and I also tried running my app with admin rights but still no use.

    this is the error I get, Access to the path 'file path' is denied.

    it tried taking ownership of folder, make it writeable and finish all security, yet  no good outcome :(

    Please provide me with a simple code which can accomplish this task.


    Afnan Makhdoom

    Monday, November 5, 2012 4:51 PM

Answers

  • Yes everything Is correct, I figured out the problem, the problem is that the folder in which I m trying to copy my resource file is also created by my app at runtime, and it gets really secured, I tried to finish its security by this code:

    Dim FolderPathAs String = "folder path" 'Specify the folder here

                Dim UserAccountAs String = "Everyone"

    Dim FolderInfoAs IO.DirectoryInfo= New IO.DirectoryInfo(FolderPath)

                Dim FolderAclAs New DirectorySecurity

                FolderAcl.AddAccessRule(New FileSystemAccessRule(UserAccount, FileSystemRights.FullControl, InheritanceFlags.ContainerInheritOr InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow))

                FolderAcl.SetAccessRuleProtection(True, False)

                FolderInfo.SetAccessControl(FolderAcl)

    but still I get, access is denied when in the next step, my app copies the (xls)resource files to the Foldermy app creates at runtime. please help me :(


    Afnan Makhdoom


    • Edited by Afnan Makhdoom Monday, November 5, 2012 10:23 PM
    • Proposed as answer by Mark Liu-lxf Tuesday, November 6, 2012 5:43 AM
    • Marked as answer by Mark Liu-lxf Thursday, November 15, 2012 6:07 AM
    Monday, November 5, 2012 10:22 PM

All replies

  • The error message you are seeing can be caused by things other than security, such as an invalid path or a bad file name. Are you sure you are providing a full path name (folder and file name) and not just the folder name as your example implies?
    • Edited by Acamar Monday, November 5, 2012 8:31 PM
    Monday, November 5, 2012 8:30 PM
  • Yes everything Is correct, I figured out the problem, the problem is that the folder in which I m trying to copy my resource file is also created by my app at runtime, and it gets really secured, I tried to finish its security by this code:

    Dim FolderPathAs String = "folder path" 'Specify the folder here

                Dim UserAccountAs String = "Everyone"

    Dim FolderInfoAs IO.DirectoryInfo= New IO.DirectoryInfo(FolderPath)

                Dim FolderAclAs New DirectorySecurity

                FolderAcl.AddAccessRule(New FileSystemAccessRule(UserAccount, FileSystemRights.FullControl, InheritanceFlags.ContainerInheritOr InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow))

                FolderAcl.SetAccessRuleProtection(True, False)

                FolderInfo.SetAccessControl(FolderAcl)

    but still I get, access is denied when in the next step, my app copies the (xls)resource files to the Foldermy app creates at runtime. please help me :(


    Afnan Makhdoom


    • Edited by Afnan Makhdoom Monday, November 5, 2012 10:23 PM
    • Proposed as answer by Mark Liu-lxf Tuesday, November 6, 2012 5:43 AM
    • Marked as answer by Mark Liu-lxf Thursday, November 15, 2012 6:07 AM
    Monday, November 5, 2012 10:22 PM
  • ok just tell me how to change the owner ship of directory to everyone in vb

    Afnan Makhdoom

    Tuesday, November 6, 2012 12:41 AM
  • My.Computer.FileSystem.WriteAllBytes("Folder Path", My.Resources._1, False)

    You don't need to change ownership of the directory!!!

    Just change the "Folder Path" to the actual file path to create this includes THE FILENAME AND EXTENSION.

    Friday, January 19, 2018 1:57 PM