Copying File to Program Directory

P

pooba53

My VB .NET application (created in VS 2003) resides in c:\Program Files
\Test\ and there's a data.mdb (Access) file within this directory.

I have a feature in my program that allows the user to import a backup
copy of this Access db if they need to.

I'm using the FileCopy method to simply copy over the data.mdb file
within the directory above. However, I get a
"System.UnauthorizedAccessException: Access to the path: c:\Program
Files\Test\data.mdb denied" when the copy is attempted.

I have admin rights to my XP computer and am wondering if this is some
sort of security issue. How does one write to the application
directory?

I'm very grateful for the help.
-Dan
 
G

Guest

It's been a while since I "played" with Access databases, but I'm wondering
if the database is in use when you try to copy it?
 
H

Herfried K. Wagner [MVP]

pooba53 said:
My VB .NET application (created in VS 2003) resides in c:\Program Files
\Test\ and there's a data.mdb (Access) file within this directory.

I have a feature in my program that allows the user to import a backup
copy of this Access db if they need to.

I'm using the FileCopy method to simply copy over the data.mdb file
within the directory above. However, I get a
"System.UnauthorizedAccessException: Access to the path: c:\Program
Files\Test\data.mdb denied" when the copy is attempted.

I have admin rights to my XP computer and am wondering if this is some
sort of security issue. How does one write to the application
directory?

With administrator privileges this should work. However, it won't work for
normal users.
 
P

pooba53

It's been a while since I "played" with Access databases, but I'm wondering
if the database is in use when you try to copy it?

There are no active transactions taking place and I can take a copy of
the data.mdb file and copy it by hand without any problems.
 
P

pooba53

FileSystem.FileCopy(OpenFileDialog1.FileName, TargetFile)

The .Filename path is correct as well as the TargetFile path when both
are sent to a msgbox() for confirmation.

-Dan
 
P

pooba53

Okay, more information...it appears the issue *is* with the database
file being used. I changed the name of the file being copied over to
something else and it works.

New question: How do I close the "interaction" between data.mdb and my
application prior to doing the filecopy? I apologize for not being an
ado.net expert ;-)

After I copy the file, I close the application and have the user
relaunch using the new database.

Thanks!
-Dan
 
P

pooba53

Tried two scenarios:

1. Closed db connections in the main form before doing the copy. No
change.

2. First I copied my imported data to the program directory under a
different name while the application is running. I thn close the
application.

When I re-launch my application, and in the formload event, I see if
the import file is present. If it is, I copy it over to the exiting
file name and location:

AppBase = AppDomain.CurrentDomain.BaseDirectory & "data.mdb"
ImportFile = AppDomain.CurrentDomain.BaseDirectory &
"data_export"

If File.Exists(ImportFile) Then
Try
FileSystem.FileCopy(ImportFile, AppBase)
File.Delete(ImportFile)
Catch
MsgBox("import of financial data did not succeed.")
End Try
End If

This all happens before I even establish a db connection. Still a no-
go ;-(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top