Question

I have a file that I want to backup, I don't have anything "touching" the file that I "know" of. But I get the message:

"The process cannot access the file 'C:\Cadence\SPB_Data\pcbenv\allegro.ilinit' because it is being used by another process

Source Code:

string fileName = @"C:\Cadence\SPB_Data\pcbenv\allegro.ilinit";
string sourcePath = @"C:\Cadence\SPB_Data\pcbenv";
string targetPath = @"C:\Cadence\SPB_Data\pcbenv\backup";

// Use Path class to manipulate file and directory paths. 
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);

// To copy a folder's contents to a new location: 
// Create a new target folder, if necessary. 
if (!System.IO.Directory.Exists(targetPath))
{
    System.IO.Directory.CreateDirectory(targetPath);
}

// To copy a file to another location and  
// overwrite the destination file if it already exists.
System.IO.File.Copy(sourceFile, destFile, true);

So as soon as the program hits this line " System.IO.File.Copy(sourceFile, destFile, true);" I get the error. Any way to "force" a copy ?

Was it helpful?

Solution 3

If the other process has marked the file as "read locked" then you are not going to be able to copy the file.

Your best bet is to figure out what process is locking the file using Process Explorer.

OTHER TIPS

Do you create this file/ write into this file in the application before you try to copy it? If you did create/write into it then you may have forgotten to close it after that.

The issue is that your source and destination for the copy operation are the same. You are using Path.Combine incorrectly. From the documentation:

If path2 includes a root, path2 is returned.

Since you have a root in path2 (the second parameter) both sourceFile and destFile are the value of fileName.

You probably want to declare string fileName = "allegro.ilinit" instead of what you have.

Obviously, the exception message is somewhat misleading.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top