Lexer

I am trying to develop an application that copies an access database file from one location to another.

I have tried using the File.copy method, but I get an empty database file in the destination folder!

Is there another method to copy the database or is it because I am doing something wronge


Re: Visual C# General Copy a Database file

Sabrecat

Im not real familiar with Access but are you copying all the files associated with the Database, and not just the .mdb file





Re: Visual C# General Copy a Database file

Chunsheng Tang - MSFT

Hi, Lexer

There is nothing special to copy access database file (even when the file is being edited in Access.). What did you mean by "an empty database file" Was it 0KB in size Couldn't you catch any exceptions Did it happen all the time or occasionally






Re: Visual C# General Copy a Database file

Peter Ritchie

You'll have to make sure your application isn't accessing that database when you perform the copy.






Re: Visual C# General Copy a Database file

Lexer

Chunsheng Tang - MSFT wrote:

What did you mean by "an empty database file" Was it 0KB in size Couldn't you catch any exceptions Did it happen all the time or occasionally



The copied file was 0KB, and I didn't catch any exceptions. I tried repeating the copy several times, but all I get is the 0KB database file!




Re: Visual C# General Copy a Database file

Lexer

I have made a small copying application. The copy result is still the same with files being copied as 0KB.



namespace CopyTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
DialogResult res = openFileDialog1.ShowDialog();

if (res == DialogResult.OK)
textBox1.Text = openFileDialog1.FileName;
}

private void button2_Click(object sender, EventArgs e)
{
DialogResult res = saveFileDialog1.ShowDialog();

if (res == DialogResult.OK)
textBox2.Text = saveFileDialog1.FileName;
}

private void button3_Click(object sender, EventArgs e)
{
String sourcePath = textBox1.Text;
String destinationpath = textBox2.Text;
try
{
using (FileStream fs = File.Create(sourcePath)) { }

//File.Delete(destinationpath);

File.Copy(sourcePath, destinationpath);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}




Re: Visual C# General Copy a Database file

Chunsheng Tang - MSFT


If you use
File.Copy(sourcePath, destinationpath);
you must make sure the destination does not exist.

Or you should force it to overwrite the destination file:
File.Copy(sourcePath, destinationpath, true);

This is true to all files not only to ms access files.