Solved

vb.net and permission issues

Posted on 2014-11-25
16
107 Views
Last Modified: 2014-12-01
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:

Dim tempFilePath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "BMWMaintenance")
        My.Computer.FileSystem.WriteAllBytes(tempFilePath, My.Resources.BMWMaintenance, False)

Open in new window


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?
0
Comment
Question by:derek7467
  • 9
  • 7
16 Comments
 

Author Comment

by:derek7467
ID: 40464574
To make this more complicated the below code works fine with UAC rights

Dim tempFilePath As String = Combine("C:\Program Files (x86)\BMW Maintenance", "BMWMaintenance.accdb")
        My.Computer.FileSystem.WriteAllBytes(tempFilePath, My.Resources.BMWMaintenance, False)

Open in new window

0
 
LVL 33

Expert Comment

by:it_saige
ID: 40464659
Try using CommonApplicationData.

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)

Open in new window


Also what is the error you are receiving?

-saige-
0
 

Author Comment

by:derek7467
ID: 40464732
error is:

An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll
Additional information: Access to the path 'C:\ProgramData\BMWMaintenance' 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
0
 

Author Comment

by:derek7467
ID: 40464947
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
0
 
LVL 33

Expert Comment

by:it_saige
ID: 40465023
I'm trying to see if I can replicate your problem.

-saige-
0
 

Author Comment

by:derek7467
ID: 40474418
any ideas why the below line always gives me a permissions error?

My.Computer.FileSystem.WriteAllBytes(tempFilePath, My.Resources.BMWMaintenance, False)

Open in new window

0
 
LVL 33

Expert Comment

by:it_saige
ID: 40474455
Personally, I have not been able to replicate this issue.  This is what I am doing:
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

Open in new window


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-
0
 

Author Comment

by:derek7467
ID: 40474521
does that work for you?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 33

Expert Comment

by:it_saige
ID: 40474535
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-
0
 

Author Comment

by:derek7467
ID: 40474562
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
0
 
LVL 33

Expert Comment

by:it_saige
ID: 40474566
I only create the file if it does not exist:
If Not database.Exists Then
	database.Create()

Open in new window

then I write the embedded resource to the file I created:
	My.Computer.FileSystem.WriteAllBytes(database.FullName, My.Resources.Testing, False)

Open in new window

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-
0
 

Author Comment

by:derek7467
ID: 40474574
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?
0
 
LVL 33

Accepted Solution

by:
it_saige earned 500 total points
ID: 40474625
Ok.  I did a little more testing.  The Create method actually opens a filestream.  So we have to close the filestream, then we can WriteAllBytes.
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"))
		Dim fileStream As FileStream
		Try
			If Not database.Exists Then
				fileStream = database.Create()
				fileStream.Close()
				My.Computer.FileSystem.WriteAllBytes(database.FullName, My.Resources.Testing, False)
			End If
		Catch ex As Exception

		End Try
	End Sub
End Class

Open in new window


As for the database resource itself.  I am simply opening the project resources designer and dragging the Database file into it directly -Capture.JPG
The database on the project is currently set with a Build Action of None and Do not copy for the Copy to Ouput Directory property -Capture.JPG
Try with your resources the way that they are first to see if it works for you.

-saige-
0
 

Author Comment

by:derek7467
ID: 40474745
that worked you da man!
0
 

Author Comment

by:derek7467
ID: 40474769
real quick if youre still here, how do i specify a folder within the localapp directory?

Dim database As New FileInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "BMWMaintenance.accdb"))

Id like to store that DB in a specific folder, say "Maintenance"
0
 
LVL 33

Expert Comment

by:it_saige
ID: 40474818
You could do something like this:
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

Open in new window


-saige-
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SlingPlayer alternatives 1 35
Authentication of Web Services 3 53
How do ASP.NET and MVC work together? 4 29
C# guarantee sql connection close 6 30
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now