1

I try to create a sub-application that copies the database to the user desired location. Although an error is popping up that my newly created folder is being used by another application (i havent used any stream readers).

The files are correct and the copy to the selected directory is totaly working , although the problem starts when i create the folder and after i try to use him.

//Snippet
string SourceFile1 = @"C:\Users\user\Documents\DLLTESTBASE.mdf";
string SourceFile2 = @"C:\Users\user\Documents\DLLTESTBASE_log.ldf";
string BackupDirectory = BackupLocation.SelectedPath + "\\" + BackupName;
if (!Directory.Exists(BackupDirectory)){
    Directory.CreateDirectory(BackupDirectory);
    }
else{
     MessageBox.Show("A copy has been found :\n" + BackupDirectory , "Copy has been stoped!");
    }

string targetPath1 = BackupDirectory + "\\DB.mdf"; 
string targetPath2 = BackupDirectory + "\\DB_log.ldf";

try{
     System.IO.File.Copy(SourceFile1, targetPath1);
     System.IO.File.Copy(SourceFile2, targetPath2);
     MessageBox.Show("Copy has been successful.", "Completed!");
    }
catch (Exception ex){
    MessageBox.Show("An error has been occured."+ex,"Operation failed!");}
    }

The result must be that the 2 files will be inside of the folder.

Mr Philip
  • 13
  • 5
  • What line throws the error? Your directory is not going to give that error because you can't lock a directory. In fact, you don't even need to call `Exists()` because `CreateDirectory()` just does nothing if the folder is already there. – Crowcoder Jan 27 '19 at 11:33
  • The error is occuring in the 1st line in Try{} – Mr Philip Jan 27 '19 at 11:35
  • 1
    off topic: you may use Path.Combine(BackupDirectory, "DB.mdf") to get targetPath1. Cleaner and you don't have to worry about slash – SZT Jan 27 '19 at 11:40
  • I'm having trouble reconciling the file is in use error with "...copy to the selected directory is totaly working". Are the databases backed by these files currently online? – Crowcoder Jan 27 '19 at 11:40
  • Could it be that the sourcefile is being used by another process? e.g. SQL server, if that is the case you cant use File.Copy - take a look at https://stackoverflow.com/questions/6167136/how-to-copy-a-file-while-it-is-being-used-by-another-process – MikNiller Jan 27 '19 at 11:42
  • Crowcoder, both files are localy (document folder) . I belive when the directory creation take place the connection/command dosnt terminated. – Mr Philip Jan 27 '19 at 11:44
  • MikNiller ,currently i have deactivated all the connections from all sql db's to test this sub-app. – Mr Philip Jan 27 '19 at 11:46
  • You are aware that just because you pop a message box that "Copy has been stopped" doesn't mean the code stops, right? Your code continues after dismissing the dialog. – Crowcoder Jan 27 '19 at 11:46
  • SZT Thank you for the tip, didnt knew that ^.^ – Mr Philip Jan 27 '19 at 11:47
  • Crowcoder ,yes i know but the error is from Catch exception not from a messagebox – Mr Philip Jan 27 '19 at 11:48
  • I didn't say the message box was the exception, it just looked like might have a logic flaw. Anyway, closing connections is not the only thing going on in a database, the engine still has a hold of the files. – Crowcoder Jan 27 '19 at 11:49
  • I have copied both files multiple times and both are working, although when i try to add them inside the folder I got the error "file is being used by another process". I really cant understand what is wrong with this one – Mr Philip Jan 27 '19 at 11:52
  • Just to make sure the sourcefile isn't actually locked by some process (you can use resource monitor to check that) , you could try using windows explorer to copy it from source dir to the backup directory – MikNiller Jan 27 '19 at 11:56
  • Didnt found any process using any of the file's. – Mr Philip Jan 27 '19 at 12:08

3 Answers3

0

Sql Data Base File in use with Sql Service

Goto Services

Stop "Sql Server" Service

you can use this link stop-or-start-sql-server-service

If u dont want to stop service use this link

Also u can use Attaching-and-Detach DB PragmaticallyAttaching-and-Detach

Mohammad Hatami
  • 299
  • 3
  • 9
0

Try the following line before you create the files: File.SetAttribute(targetpath1, FileAttribute.Normal);

You will get an exception thrown if the files already exist.

You will need to either delete the files and then write to them or use overwrite parameter: System.IO.File.Copy(sourcefile1, targetPath1, true);

0

Sorry for late responce, as it seem the problem was occuring beacause of a hidden compartment of my main application, the problem solved after restarting my computer and reappeared when i runned the main application so you were right guys that sql file-connection was running (although it wasnt visible).

Thank you everyone for the help ☺

Mr Philip
  • 13
  • 5