File.Copy does not release the lock on the copied file.

 Hi!

I am trying to use the File.Copy function in order to copy a file from the Client's PC to the Server. The File to be copied is selected by the user using the FileUpload control and is then used by the server to perform certain operations.

My problem is that when I copy the file using the File.Copy method and then execute a streamreader process on the file straight after, I get the following error;

"The process cannot access the file '\\syddev1\latest\xmasEmail.html' because it is being used by another process. "

Below is the piece of code where the problem occurs;

//FupHTML is the FileUpload control in ASP.NET.

File.Copy(FupHTML.PostedFile.FileName, @"\\syddev1\latest\" + FupHTML.FileName ,true);

string Path = @"\\syddev1\latest\" + FupHTML.FileName; //System.IO.Path.GetFullPath(FupHTML.PostedFile.FileName);

objStreamReader = File.OpenText(Path);

Any ideas on how I can unlock/release the file after it has been copied?

Regards

Vik


Sakiv
0
Sakiv
1/20/2009 5:41:43 AM
📁 asp.net.web-forms
📃 93655 articles.
⭐ 5 followers.

💬 5 Replies
👁️‍🗨️ 2487 Views

Hi Sakiv,

I guess my first question would be why are you using File.Copy instead of the FileUpload.SaveAs() method?

I believe it's mostly up to the Operating System how long the file lock is placed on the file. If you're hard set on keeping the File.Copy code, you might want to think about wrapping the task in using statement, i believe File inherits IDisposable.  once the using statement runs it calls the Garbage Collector to clean up the internal resources, which should hopefully release the lock from the file.

Hope this helps.


Happy Coding,

SyntaxC4

*** REMINDER ***
If you find this post useful, Please click the 'Mark as Answer' Button.

*** DISCLAIMER ***
All Code is provided AS IS.

** Want Some Useful Sites **

http://www.delicious.com/SyntaxC4
0
SyntaxC4
1/20/2009 7:05:53 AM

 HEy Sakiv,

I think you are doing correct coding but add one line befiore you copied ur file.... Notice below the deleting of an existing file.

  

1     //Attempt file copy
2        try 
3        {
4    
5          //If destinaltionFile already exists then it causes exception
6          //So delete it first to overrite
7          File.Delete(destinationFile);
8    
9          File.Copy(sourceFile, destinationFile);
10         Console.WriteLine("Copy completed.");
11       } 
12   
13       
14       catch (Exception ex)
15       {
16         Console.WriteLine(ex.Message);
17       }
18   
I hope this should help you

"Mark as Answer" on the post that helped you.

Chandan,
Imfinity India Pte Ltd.
0
chandan
1/20/2009 7:26:28 AM

Use FileUploadControl.SaveAs function to save the file at the desired location.

 

Thanks

 


Thanks & Regards,
Izhar Ul Islam Khan
Microsoft Certified Technology Specialist
0
izharulislam
1/20/2009 5:47:19 PM

Thanks for all your help guys. The File.SaveAs method did the trick.

 Regards

Vik


Sakiv
0
Sakiv
1/20/2009 11:23:56 PM

Hi Sakiv,

Please mark the useful posts as the answer to help other developers having a similiar issue.

Thanks!


Happy Coding,

SyntaxC4

*** REMINDER ***
If you find this post useful, Please click the 'Mark as Answer' Button.

*** DISCLAIMER ***
All Code is provided AS IS.

** Want Some Useful Sites **

http://www.delicious.com/SyntaxC4
0
SyntaxC4
1/21/2009 2:15:24 AM
Reply:
(Thread closed)