I hope some can help me.

I need to rename a file. I can't use a move or copy and delete, as these are too slow. The folder is being monitored and as soon as the file entry appears in the directory the expectation is that the file is complete and ready for processing. The monitored could be anywhere on the network.

At the moment I am using :

Microsoft.VisualBasic.FileSystem.Rename(tempFile, filename);

And I have tried :

File.Move(tempFile, filename);

I don't know whether Move (C#) or VB.Net FileSystem.Rename equates to Copy and Delete underneath.

I'm using C# 2.0.

Any help would be greatly appreciated.



Re: Visual C# General Renaming a file

Tom Meschter MSFT

Microsoft.VisualBasic.FileSystem.Rename() and File.Move() will almost certainly have to do a "copy and delete" if the file is being moved to a different drive or network share. If you're just renaming it in the same directory I certainly hope it doesn't have to do that, but I can't offer you any guarantees beyond what is given in the documentation for those two methods.

Have you established through profiling or other metrics that these methods are too slow for your needs

-Tom Meschter
Software Dev, Visual C# IDE

Re: Visual C# General Renaming a file

Matthew Watson

If you use Reflector to inspect the code for File.Move(), you will discover that ultimately it calls the Windows API MoveFile() function.

The Windows API MoveFile() function does the most efficient thing when "Moving" on the same drive, i.e. it just updates the directory entries without copying the contents of the file.

Re: Visual C# General Renaming a file


Thanks Tom and Matthew.

Speed is definately an issue! The monitoring app is so quick that it starts process the file as soon as the directory entry is created regardless of whether the file is till being written. The only way we could resolve the problem (vb6) was to create the file with temp name in the correct folder then rename. Originally we used to copy the file into the correct folder, but found that processing started before the copy was complete. The monitored folder is a on a remote machine.

Is there any way I can get at the OS rename command I suppose I could create a .bat file and Process it.