Home All Groups Group Topic Archive Search About

Implementing renaming of a file

Author
14 Jun 2006 7:23 PM
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

Author
14 Jun 2006 7:30 PM
GhostInAK
Hello Emmanuel Stapf [ES],

Pseudo code:

if sourcePath == targetPath Then Do Nothing, End, Exit, Die
if sourcePath does not exist Then Maim the user, End, Exit, Die
if targetPath exists Then delete targetPath
Wow, the user really knew what they were doing?  Great.. File.Move the suckah,
err, sourcePath.

-Boo


Show quote
> 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
Author
14 Jun 2006 8:15 PM
Markus Stoeger
GhostInAK wrote:
> 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
Author
15 Jun 2006 3:24 AM
Jeffrey Tan[MSFT]
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.
Author
19 Jun 2006 7:42 AM
GhostInAK
Hello Jeffrey Tan[MSFT],

Good call on the GetLongPathName.

-Boo

Show quote
> 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.
Author
19 Jun 2006 8:09 AM
Jeffrey Tan[MSFT]
:-)

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.

AddThis Social Bookmark Button