netcat
netcat

Reputation: 1335

Does File.Copy prevent another app from writing to the file?

I want to copy some text files, that are written by another app, but I don't want to do anything to prevent the other app. from writing to these files.

I am using File.Copy, from the System.IO namespace, C#, .Net Framework 2.0.

I checked MSDN's documentation, but nothing is specifically stated about the method that File.Copy uses. Is it a wrapper to an unmanaged API call?

Does File.Copy lock, or block the file being copied in any way?

Thanks in advance for any info about this.

Upvotes: 7

Views: 487

Answers (2)

Andrii Startsev
Andrii Startsev

Reputation: 767

You can use .Net Reflector (or other decompile tool) to look at internal structure of the method.

Actually you can see that System.IO File.Copy uses Microsoft.Win32.Win32Native library for coping files. Microsoft.Win32.Win32Native it is CLR wrapper for all Win32 native operations.

Upvotes: 1

schglurps
schglurps

Reputation: 1387

If the other app creates its files by specifying FileShare.None, you will receive an UnauthorizedAccessException. So I imagine that you just have to process this specific exception in your app.

Upvotes: 2

Related Questions