locked
Can’t copy a file from one folder to another.

    Question

  • I have a DLL program; one of its functions is to copy a file from one folder to another. When this program is running under Vista or Windows7, it denies that process. I had a similar problem with an EXE program but sort-of fixed it by modifying the UAC under applivcation setting within VB. I just can’t figure out a way to do this for a DLL file. Maybe there is a different approach. I have played around with the “ SecurityPermisoinAttribute ” but no luck. I have provided the code below. Thanks in advance for your help.


    Imports System.Security.Permissions
    
    <Assembly: SecurityPermissionAttribute(SecurityAction.RequestOptional, UnmanagedCode:=True)> 
    
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim fp As New FileIOPermission(FileIOPermissionAccess.Write, "C:\Program Files\Folder")
            fp.Assert()
            Try
                Dim FileBytes() As Byte
                FileBytes = System.IO.File.ReadAllBytes("C:\Program Files\Folder\The File.dwg")
                System.IO.File.WriteAllBytes("C:\Program Files\Folder\The File.dwg", FileBytes)
            Catch ex As Exception
                MsgBox(ex.Message.ToString)
            End Try
        End Sub
    End Class
    
    
    

     

     

    Friday, January 15, 2010 4:48 PM

Answers

  • Thanks for your help, it wasn't a direct answer but I figured out that I can avoid this whole problem just by saving program data to the current/users/app.data.

    My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData
    • Marked as answer by Apopka_Pilot Friday, January 15, 2010 8:05 PM
    Friday, January 15, 2010 8:05 PM

All replies

  • If you are running Windows 7, try running the program with admimistrative privileges. That will tell you if there a file protection problem.
    Renee 
    • Proposed as answer by Rudedog2MVP Friday, January 15, 2010 7:16 PM
    Friday, January 15, 2010 5:14 PM
  • You are accessing a sensitive area on the disk, the ProgramFiles folder.

    Are try to open a file in a folder different from the output folder?
    Are you running in Debug or Release mode?


    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, January 15, 2010 5:42 PM
  • I'm running in release mode. I'm trying to copy a file from one folder to another
    Friday, January 15, 2010 5:54 PM
  •  I'm trying to copy a file from one folder to another
     
    Really? 
    I would have never guessed that.

    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, January 15, 2010 5:58 PM
  • I've thrown everything but the kitchen sink @ this issue. I won't give up but man, it has been a challange. I would really thing that the piece of code that I have provided would have worked but I was wrong.
    Friday, January 15, 2010 6:02 PM
  • You are accessing a sensitive area on the disk, the ProgramFiles folder.

    Are try to open a file in a folder different from the output folder?



    Third attempt.  It's Friday. 

    That means the exe is in one folder.
    Are you trying to copy into or out of the folder where your EXE is located?
    Or are you trying to reference two entirely different folders.

    Is your EXE located within Program Files ?

    Do your realize that your code cannot access other folders located within Program Files?

    =|^D

    Mark the best replies as answers. "Fooling computers since 1971."


    EDIT:
    I have a habit of ignoring questions from people who ignore mine. 
    Hey, fair is fair.
    Friday, January 15, 2010 6:05 PM
  • Well sort-of ;), I'm trying to copy a file from a folder under Program Files to a different folder under Program Files. Did that answer your questions?



    Friday, January 15, 2010 6:10 PM

  • Yes that answered my question. 
    It is exactly as I had expected.  Evasive.   "Well sort of ...."

    I already posted of couple of answers to your problem.

    You are accessing a sensitive area on the disk, the ProgramFiles folder.

    Do your realize that your code cannot access other folders located within Program Files?

    Care to venture a guess as to why these restrictions might be in place?

    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, January 15, 2010 6:16 PM
  • Yes, My program is under Program Files. I'm trying to copy from a different location under Program Files to a subfolder under my programs folder under Program Files.

    Example:

    My program is located here: c:\progrm files\my program folder\

    I want to copy from c:\progrm files\a different program folder\ to c:\progrm files\my program folder\subfolder\

    Friday, January 15, 2010 6:20 PM
  • You seem to read and respond to only half of what I write, the first half. 
    Let me repeat the parts you missed or ignored.

    /****************************************************************************************/

    You are accessing a sensitive area on the disk, the ProgramFiles folder.
    Do you realize that your code cannot access other folders located within Program Files?

    Care to venture a guess as to why these restrictions might be in place?

    /****************************************************************************************/



    Mark the best replies as answers. "Fooling computers since 1971."


    EDIT:
    It's Friday, and your 3 strikes are up.  I am quitting this thread.
    Friday, January 15, 2010 6:24 PM

  • I apoligies. I now realize that It can't access other folder in program files.


    I guess because of security reasons.. Thats a wild guess.

    Friday, January 15, 2010 6:33 PM




  • I guess because of security reasons.. Thats a wild guess.



    Trumpets, please.  TAA DAA. 

    Your guess didn't have to be all that wild.  Of course it is because of security reasons once you slow down, stop and think before you leap.  Programs that have been installed on your computer must be protected from being accessed by other programs.  The intent of this security policy is to protect you from viruses.

    Since your code cannot access the areas where executable program files are stored, the area of your Hard Drive known as "My Documents" was created to allow different programs to read and write to a common area that the user could access.

    Happy Coding

    Rudy  =8^D

    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, January 15, 2010 6:44 PM
  • Thanks for your help, it wasn't a direct answer but I figured out that I can avoid this whole problem just by saving program data to the current/users/app.data.

    My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData
    • Marked as answer by Apopka_Pilot Friday, January 15, 2010 8:05 PM
    Friday, January 15, 2010 8:05 PM
  • Thanks for your help, it wasn't a direct answer but I figured out that I can avoid this whole problem just by saving program data to the current/users/app.data.

    My
    .Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData
    


    You never asked anything about saving program data.
    You asked about copying files from one folder to another.

    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, January 15, 2010 8:27 PM