0

im trying to save an access database file by creating a new database file and save it anywhere as the user like and also name the file.

i tried using this code but it say 'the file cannot be access as it use by another process'. any idea?

private void btnSelectDatabase_Click(object sender, EventArgs e)
    {
        ofdMain.ShowDialog();
        lblDatabase.Text = ofdMain.FileName;
    }

    private void btnLoadDatabase_Click(object sender, EventArgs e)
    {
        try
        {
            test = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;
            Data Source=" + lblDatabase.Text);
            test.Open();
            refresh();
        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void btnSave_Click_1(object sender, EventArgs e)
    {
        sfdMain.ShowDialog();
    }

    private void sfdMain_FileOk(object sender, CancelEventArgs e)
    {

        StreamReader read = new StreamReader(lblDatabase.Text);
        Stream s = File.Open(sfdMain.FileName, FileMode.CreateNew);
        StreamWriter writer = new StreamWriter(s);
        writer.Close();

    }
  • You are using `StreamReader` and `StreamWriter` to copy files? Would it not be easier to use `File.Copy(SourceFile, TargetFile);`? – Luke Wage Jul 18 '13 at 08:04
  • Yes - the file 'd been opened by this or another process. - What do you expect us to do? Hint: tell more about it (what are you doing and how, where does the exception occur a.s.o.) – JeffRSon Jul 18 '13 at 08:04
  • @LukeWage i want to do that way but the file is yet to be created. so basically there is no target file. – cSharpNoobies Jul 18 '13 at 08:18
  • @cSharpNoobies: The target file does not have to exist. Quite the opposite: if it exists, you will get an error. Here is some more information: [link](http://msdn.microsoft.com/en-us/library/c6cfw35a.aspx) – Luke Wage Jul 18 '13 at 08:21
  • @JeffRSon im still new in c# and im still dont know much about it so i expect you guys can tell me the right way to this. i tried refer to books but cant find the solution. i tried to copy the access database file opened by the user and save it in another access database file by creating a new file. i hope you guys can understand. T.T – cSharpNoobies Jul 18 '13 at 08:24
  • @LukeWage thanks!! that link helps a lot! :D – cSharpNoobies Jul 18 '13 at 08:26

0 Answers0