derek7467
asked on
vb.net and permission issues
I wrote a small program and it takes an access database stored as a resource in my.resources. On load im telling it to take that db and put it on the local workstation with the below code:
IM not sure why but i keep erroring out with a permissions error telling me i dont have rights. I tried launching as an admin and i still get the same result. I am an admin of my machine and a domain admin so this should work. What could be wrong?
Dim tempFilePath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "BMWMaintenance")
My.Computer.FileSystem.WriteAllBytes(tempFilePath, My.Resources.BMWMaintenance, False)
IM not sure why but i keep erroring out with a permissions error telling me i dont have rights. I tried launching as an admin and i still get the same result. I am an admin of my machine and a domain admin so this should work. What could be wrong?
Try using CommonApplicationData.
Also what is the error you are receiving?
-saige-
The directory that serves as a common repository for application-specific data that is used by all users.Source
Dim tempFilePath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "BMWMaintenance")
My.Computer.FileSystem.WriteAllBytes(tempFilePath, My.Resources.BMWMaintenance, False)
Also what is the error you are receiving?
-saige-
ASKER
error is:
An unhandled exception of type 'System.UnauthorizedAccess Exception' occurred in mscorlib.dll
Additional information: Access to the path 'C:\ProgramData\BMWMainten ance' is denied
This is with UAC rights built in through app manifest.
Weird thing is i can create the folder fine, its when it tries to copy the db over i get the error
An unhandled exception of type 'System.UnauthorizedAccess
Additional information: Access to the path 'C:\ProgramData\BMWMainten
This is with UAC rights built in through app manifest.
Weird thing is i can create the folder fine, its when it tries to copy the db over i get the error
ASKER
Is there any other way to copy an access db file to another directory? Right now, im embedding it in my exe and distrbuting it from my.resources
I'm trying to see if I can replicate your problem.
-saige-
-saige-
ASKER
any ideas why the below line always gives me a permissions error?
My.Computer.FileSystem.WriteAllBytes(tempFilePath, My.Resources.BMWMaintenance, False)
Personally, I have not been able to replicate this issue. This is what I am doing:
Not that this will make any difference because it does nothing for the security.
However, maybe an application manifest will help:
http://msdn.microsoft.com/en-us/library/bb756929.aspx
-saige-
Imports System.IO
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim database As New FileInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Testing.sdf"))
If Not database.Exists Then
database.Create()
My.Computer.FileSystem.WriteAllBytes(database.FullName, My.Resources.Testing, False)
End If
End Sub
End Class
Not that this will make any difference because it does nothing for the security.
However, maybe an application manifest will help:
http://msdn.microsoft.com/en-us/library/bb756929.aspx
-saige-
ASKER
does that work for you?
The code, as posted, works for me without any issues. I do not have to create an application manifest. However, if you are still presented with a problem, then you might try the application manifest route.
-saige-
-saige-
ASKER
it looks like yours is just creating a file, i am trying to copy over an access database that i embedded in my visual studio resources
I only create the file if it does not exist:
-saige-
If Not database.Exists Then
database.Create()
then I write the embedded resource to the file I created:
My.Computer.FileSystem.WriteAllBytes(database.FullName, My.Resources.Testing, False)
But as I stated, doing this does not resolve issues with access rights. It's only another method of doing the same thing you are.-saige-
ASKER
Well doing it your way actually works without errors, but the file is 0 bytes, meaning nothing was written to it. How should i store my access db in the resource section? What should the build action be set to?
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
that worked you da man!
ASKER
real quick if youre still here, how do i specify a folder within the localapp directory?
Dim database As New FileInfo(Path.Combine(Envi ronment.Ge tFolderPat h(Environm ent.Specia lFolder.Lo calApplica tionData), "BMWMaintenance.accdb"))
Id like to store that DB in a specific folder, say "Maintenance"
Dim database As New FileInfo(Path.Combine(Envi
Id like to store that DB in a specific folder, say "Maintenance"
You could do something like this:
-saige-
Imports System.IO
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim location As New DirectoryInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Maintenance"))
Dim database As New FileInfo(Path.Combine(location.FullName, "Testing.sdf"))
Dim fileStream As FileStream
Try
If Not location.Exists Then location.Create()
If Not database.Exists Then
fileStream = database.Create()
fileStream.Close()
End If
My.Computer.FileSystem.WriteAllBytes(database.FullName, My.Resources.Testing, False)
Catch ex As Exception
End Try
End Sub
End Class
-saige-
ASKER
Open in new window