# File.Move in C# possibly locking the file?



## kadamczy (Jan 13, 2011)

This is an odd thing I've encountered just recently and I cannot find a root cause for the problem. I have a program that essentially takes a document and moves it to another location (with several bells and whistles).

This program has run flawlessly over the past couple months, but now it seems to be causing the files to be locked. It says: "Access denied. Contact your administrator.". The original submitter can open the file but nobody else

The file is moving from an unknown location (wherever the end user has the file) and moving it to a network shared drive. Could the file.move cause this? Here's the code snippit

(for corporate purposes I replaced the path with {servername})

```
var newDirectory = "\\\\{Servername}\\" + txtDocName.Text + "\\"+cbDocType.Text;
Directory.CreateDirectory(newDirectory ); //Create the directory
try
{
  File.Move(_path, newDirectory +"\\"+ txtFilename.Text);      //Move the file to the new directory
  _path = dc + "\\" + txtFilename.Text;     //Update new path global
  Sendmail();   //Send the confirmation email
}
```
Here are my theories and what I've done to try to solve them


The file is encrypted before the transfer
I had the file resaved by the author. This was still locked

The file carried over permissions from the local drive
I had the file resaved by the author then moved using windows explorer to the parent folder. This unlocked it.

The file was open when C# tried to move it
The file will not move and throws a IOexception

Is this caused by the File.Move function? is there a simple alternative for the filemove? Should I just give up using this an just use StreamWriter? Any assistance would be greatly appreciated.

Thanks!


----------



## -Fabez- (Jul 28, 2008)

Did you change any code, files or settings before this started happening ? Try using Unlocker to find out what is causing the locking, which will make it a lot easier to diagnose the problem.


----------



## kadamczy (Jan 13, 2011)

I had not changed any code at that point in time, I thought they might've somehow accidentally installed my beta build but I verified their versions against the release. Unlocker did not seem to work on it (I have never had much luck with that since it's a network file and I'm not sysadmin)


----------



## -Fabez- (Jul 28, 2008)

Have you tried asking your system admin to try Unlocker and can they change the owner themselves ?


----------

