Implementing renaming of a file

  • Thread starter Thread starter Emmanuel Stapf [ES]
  • Start date Start date
E

Emmanuel Stapf [ES]

Hi,

I'd like to implement `rename' as it is done on Unix platforms. There is
FileInfo.MoveTo, but if the destination file already exist it does not do
the renaming.

I thought of doing something like (pseudo-code):

if (destination_exists) {
delete destination
}
FileInfo.MoveTo (destination)

but this does not work when source and destination are the same file. On
unix in that case, `rename' is a no-op.

I could not find a way that is guaranteed to work accross all and future
versions of the .NET framework.

Any help appreciated,
Regards,
Manu
 
GhostInAK said:
Hello Emmanuel Stapf [ES],

Pseudo code:

if sourcePath == targetPath Then Do Nothing, End, Exit, Die

I think it's not so easy. Reason #1: some characters in the sourcePath
might have another upper/lower case than in the targetPath, yet point to
the same file. You'd have to do an case insensitive compare.

Reason #2: 8.3 file names still exist. Source path might be in 8.3
format, while the target path might be a new long file name. Again, both
pointing to the same file.

I'd try opening both files with FileShare.None at the same time. If the
method doesn't fail you know that the files must be different.

hth,
Max
 
Hi Markus,

Thank you for reminding us this issue.

#1, this is easy to use String.ToLower to compare these 2 paths in
lowercase(case-insensitive comparison).
#2, we'd better p/invoke GetLongPathName win32 API to get the long file
name format, then we can do the case-insensitive comparison now. Note:
based on my test, if we input a long file name for GetLongPathName API, it
will not fail and just return the same long file name, so our logic will
not break.

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
:-)

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top