How to Copy a file

  • Thread starter Thread starter Tom
  • Start date Start date
T

Tom

I am having trouble when I read a file and another process
is trying to update it.

So I need a rountine to copy a file in asp.net.

Can anyone assist?

Thanks

Tom
 
using System.IO;

....

public static bool copyFile
(string source, string destination, bool overwrite) {
File.Copy(source, destination, overwrite);
}
 
Hi,

Try this.

C# code example ;
----------------
The use the System.IO namespace

File.Copy(@"C:\MyFile.Txt", @"C:\MyOtherDir\MyFile.Txt");
//To copy to a different file name is also possible
File.Copy(@"C:\MyFile.Txt",
@"C:\MyOtherDir\MyNewFileName.Txt");
 
Hi Tom,

If you are able to copy the file manually in Windows Explorer, ie
the Windows Shell can do it, try invoking Shell from your program.

Process.Start() is one area you might look at.

Regards,
Fergus
 
Tom wrote:
|| Chris:
||
|| Thanks for that but I'm still having trouble.
||
|| See I wish to access a file (CrawlerStatus.txt) to see if
|| the search is done. CrawlerStatus.txt is a text file that
|| gets updated by a third party software.
||
|| Anyway, when I used StreamReader object, to get the
|| contents WHILE the process was updating the file, I got an
|| error.
||
|| So I thought copying it and then use StreamReader to query
|| the copied file contents would do the trick. But I'm
|| getting the same error.
||
|| So do you know of a way to access a file while its being
|| updated so as to not harm it?
||
|| TOm

You can't copy the file simply because the thrird party didn't open the file with shared read access.

Willy.
 
Hi!

Apparently the third party software doesnt allow the file to be read when
being updated. There's nothing that you can do about it.

So, the best bet is to poll the file status and when its free, you should
proceed to read it.
 
I did a little more research, and I believe I found what you need.

IF YOUR application is the one that's writing to the file, you should use
share access. That is, when you open the file to write to it, you can
specify several different file modes, and one of them is sharing. Use
System.IO.FileShare.Read to be enable it to share with other processes that
wish to read from the file while it's being updated.

on the other hand, if you don't have the ability to change the code of the
program that's writing to the file, then you will just have to continue to
try to open it until that process is done with the file. If the process
that's writing the file never releases control of the file, then it's not
possible (as far as I know).



Chris LaJoie
 
Back
Top