1

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?

Jongware
  • 22,200
  • 8
  • 54
  • 100
Galacticai
  • 87
  • 1
  • 13

1 Answers1

2

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.

Fᴀʀʜᴀɴ Aɴᴀᴍ
  • 6,131
  • 5
  • 31
  • 52
  • I have already tested this before using the last code, ill test again.. maybe I missed something in the code – Galacticai Oct 25 '15 at 08:37
  • @NHK it is the problem mentioned in my answer which causes the error. Try with admin privilleges : `File.WriteAllBytes("D:\", My.Resources.exe1)`. Even this will throw the 'Access Denied' exception. But try "D:\exe1" and it works like a charm – Fᴀʀʜᴀɴ Aɴᴀᴍ Oct 25 '15 at 11:08
  • worked finally, probably I was missing the \ character. I used the code that u posted above – Galacticai Oct 25 '15 at 13:27