Detection if File.Move requires “Copy & # 8594; Delete” or simply change the location in the file system table

Disclaimer: I immediately admit that I have quite a lot of ignorance regarding the details of how file systems work. I have been using NTFS for so long that I can extrapolate what happens based on the behavior I observed, as well as everything that I learned from dorking on the Internet - however ...

I was hoping there was a way to detect if, when moving a file from "location A" to "location B", whether the operation would require the equivalent of "File.Copy → File.Delete" or if it does not copy the actual data of the file, but simply updates the location in the "main file table", etc.

For various purposes, I sometimes move a large number of large files. I like to report progress in the user interface.

I understand that when I call File.Move and move the file from one place to one drive / partition to another, the function will effectively "copy file data → delete" between drives / partitions. When he encounters this situation, I want to know that this will happen, so I can use the code I created that will copy the file and provide a detailed progress report as the bytes are transmitted, so that I can update progress indicators in the user interface often .

When it is on the same drive / partition, since updating the location in the main file table is so fast, I just update the progress bar when the file.Move function completes for every file I move with.

Using .NET 4 (C #), is it possible to detect using other means whether the File.Move call requires the equivalent operation “copy → delete” or simply updates the file table rather than copying the file data?

Edit:

As I noted in the comments below, the possibilities of how I thought this could be done is to determine whether the location of the source file and the location of the target file are on the same physical disk and partition, and if so, it would mean I could accurately predict the behavior and decide which functions to call - the built-in File.Move function for the "file system of the file system", which, it seems to me, occurs when moving to the same disk / part or my more detailed report about each 'x' bytes copied 'copy code n stom file. File transfer speeds can vary greatly on the machines on which my programs will run, so I would like to report on the results achieved / current transfer speed when possible.

Note. The program can use network UNC paths that can use different physical disks with one root path, that is: \\ somename \ shares \ workfolder \ project can be on another physical disk, and then \\ somename \ shares \ workfoldder \ otherproject. Therefore, I need a method for detecting a partition identifier or physical disk identifier to find out if the source and destination folders are on the same drive / partition.

thanks

+6
source share
6 answers

You can always P / Invoke on MoveFileWithProgress . From a quick read, it looks like it will give you more detailed progress if the file is copied rather than moved.

An alternative is always SHFileOperation , which will give you the interface and semantics of Windows Explorer.

+2
source

I do not think that you can discover such a thing in the usual way, since you should not care. If you could check this, what would be the case of if and else, since it is impossible to extract knowledge from this (except for the transmission rate).

In .net, the file system is abstracted, so you can simply move files from one place to another without knowing which partition / file system / network drive you are copying from / to.

For your specific problem, why not show the progress bar and update it at a specific time interval for both cases.

+1
source

I am sure you cannot do this.

For beginners, even on NTFS, just looking at the drive letter for local drives is not enough, because you can mount the drive in an empty folder on an existing drive . This means that even if two paths have the same drive letter, they can still be installed on different drives.

To take it one step further, consider external stocks, possibly installed on a nix-based system and opened through Samba. * Nix symbolic links do not allow you to determine if two paths are on the same physical device, looking only at the paths - the only option is to ask the remote machine, “do these two paths exist on the same drive?”, Something that I really doubt what will be exposed.

I would just use File.Move and agree that you are not getting progress updates. In any case, writing my own implementation of the copy amazes me as "not a particularly good idea."

+1
source

Although not 100% correct (sym-links pretends that things are one large section, while they can be different physical disks (from the top of the head, like WHS, is used extensively), I would say for purposes a progress indicator assuming that staying on the same partition (System.IO.Path.GetPathRoot) is likely to be enough.

0
source

I don’t know anything about transfer speed and execution speed, but when you discover whether a file is moving to another drive / partition, you can simply compare the drive / partition letter of the destination and the source file ...

if you use full paths for File.Move, you can use something as simple as:

 if(sourcePath[0]!=destinationPath[0]) //enable transfer speed monitoring 

if you use relative paths, you need to extract the target / source disk / partition by interpreting more lines.

Sorry if my solution is too simple or if I do not see any other problem.

0
source

You can P / call the GetFileInformationByHandle () function, which returns the serial number of the volume. If the serial number of the volume is the same for both files, they are on the same volume and copying and deletion are not required.

0
source

Source: https://habr.com/ru/post/896853/


All Articles