0

I transfer file from computer to computer Through a WebService.

After the file was transfer I need to read its size.

When I try to read the file size I get:

The process cannot access the file XXX.XXX because it is being used by another process

What can I do to avoid this error?

i read the file like this:

 LocalFileSize = File.Open(TermSendName, FileMode.Open, FileAccess.Read).Length.ToString();

how to close this ?

Gali
  • 14,511
  • 28
  • 80
  • 105
  • 1
    Please post your current code - you are leaving a handle to the file open, and we need to know where it is. – RB. Aug 10 '11 at 07:29
  • Did you try to use FileInfo.Length property http://msdn.microsoft.com/en-us/library/system.io.fileinfo.length.aspx. I think it should work with open files. – Santhosh Aug 10 '11 at 07:36

3 Answers3

1

I guess that you are using a FileStream to read the file.

Close the stream when the transfer has completed.

jgauffin
  • 99,844
  • 45
  • 235
  • 372
0

Please check the following Stack Overflow link, I faced the same issue and got fixed here:

The process cannot access the file because it is being used by another process.c#

Community
  • 1
  • 1
Shashikant
  • 137
  • 1
  • 2
  • 10
0

Try this

FileStream stream = File.Open(TermSendName, FileMode.Open, FileAccess.Read);

LocalFileSize = stream.Length.ToString();

stream.Close();

If you only want to get length of file you can also do something like

FileInfo fileInfo = new FileInfo(fileName);
string size = fileInfo.Length.ToString();
Haris Hasan
  • 29,856
  • 10
  • 92
  • 122