0
votes

i'm using VB.NET language on windows 10 with VS 2015

I'm trying to make a directory then copy a file from my app's resources folder to that directory

Code:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim SubFolderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "Main Folder\Sub Folder")

    Directory.CreateDirectory(SubFolderPath)

    'Error: access denied to "C:\Program Files\Main Folder\Sub Folder"
    File.WriteAllBytes(SubFolderPath, My.Resources.exe1)
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    File.WriteAllBytes(SubFolderPath, My.Resources.exe2)
    File.WriteAllBytes(SubFolderPath, My.Resources.exe2dat)
End Sub

i get error as commented in the above code, (i have admin rights)

Code result: created folder "C:\Program Files\Main Folder\Sub Folder" but then access denied while copying.

i'm not knowing why access is denied... can you help me please?

1
This is only a guess... Have you tried setting the directories to not be read-only?Kyle Williamson
@KyleWilliamson nope.. how? when i enter folder settings (in windows explorer) "reed-only" option is selected automatically even if i remove it..NHK
Have you tried adding a Backslash to the end of "Sub Folder"?Kyle Williamson
@Kyle the same happenedNHK
Okay. Trying to work this out with you. Have you tried creating a directory in a different area? Maybe MyDocuments?Kyle Williamson

1 Answers

2
votes

The problem with your code is that you specify a directory name instead of a file name as the first argument of the File.WriteAllBytes methods:

File.WriteAllBytes(SubFolderPath, My.Resources.exe1)`

Do something like this to correct it:

File.WriteAllBytes(SubFolderPath & "\exe1.exe", My.Resources.exe1)
File.WriteAllBytes(SubFolderPath & "\exe2.exe", My.Resources.exe2)
File.WriteAllBytes(SubFolderPath & "\exe2dat.dat", My.Resources.exe2dat)

And it's not a problem with Byte(). Whenever you import a binary exe to your resources, it is stored as a Byte(). You need not worry about that.