System.IO.File封装了SMB1.0/SMB2.0吗?

huangapple go评论78阅读模式
英文:

Does System.IO.File encapsulates SMB1.0/SMB2.0?

问题

  1. System.IO.File并未封装SMB1.0/SMB2.0协议,因此您需要使用第三方SMBLibrary来执行这些操作。

  2. 您的代码段似乎只是重命名文件而不是实际移动文件。要移动文件,您需要执行以下操作:

public bool Move(string sourceFilePath, string targetFilePath)
{
    if (!CheckLoggedIn())
    {
        return false;
    }

    object sourceFileHandle = null;
    object targetFileHandle = null;

    _status = _store.CreateFile(out sourceFileHandle,
                               out FileStatus _,
                               sourceFilePath,
                               AccessMask.GENERIC_ALL | AccessMask.SYNCHRONIZE,
                               FileAttributes.Normal,
                               ShareAccess.Read,
                               CreateDisposition.FILE_OPEN,
                               CreateOptions.FILE_NON_DIRECTORY_FILE | CreateOptions.FILE_SYNCHRONOUS_IO_ALERT,
                               null);

    if (_status == NTStatus.STATUS_SUCCESS)
    {
        _status = _store.CreateFile(out targetFileHandle,
                                   out FileStatus _,
                                   targetFilePath,
                                   AccessMask.GENERIC_ALL | AccessMask.SYNCHRONIZE,
                                   FileAttributes.Normal,
                                   ShareAccess.Write,
                                   CreateDisposition.FILE_CREATE,
                                   CreateOptions.FILE_NON_DIRECTORY_FILE | CreateOptions.FILE_SYNCHRONOUS_IO_ALERT,
                                   null);

        if (_status == NTStatus.STATUS_SUCCESS)
        {
            _status = _store.CopyFile(sourceFileHandle, targetFileHandle);
            if (_status == NTStatus.STATUS_SUCCESS)
            {
                _status = _store.DeleteFile(sourceFileHandle);
            }
        }
    }

    return _status == NTStatus.STATUS_SUCCESS;
}

请注意,上述代码会打开源文件和目标文件,然后复制源文件内容到目标文件,最后删除源文件,从而实现文件的移动操作。

英文:

I am new to SMB1.0/SMB2.0 and am trying to create a C# class library to encapsulate basic File/Directory operations like Copy, ReadAllText, Delete, etc., using <https://github.com/TalAloni/SMBLibrary>.

My questions are:

  1. Is System.IO.File already encapsulate SMB1.0/SMB2.0 protocol, and do I not need to use 3rd party SMBLibrary?

  2. I have written the following code to Move a file from one SMB share to another SMB share, but it only renames and does not Move the file from a source location to the target location. How can I do that?

  public bool Move(string sourceFilePath, string targetFilePath)
        {
            if (!CheckLoggedIn())
            {
                return false;
            }

            object fileHandle = null;
            _status = _store.CreateFile(out fileHandle,
                                       out FileStatus _,
                                       sourceFilePath,
                                       AccessMask.GENERIC_ALL | AccessMask.SYNCHRONIZE,
                                       FileAttributes.Normal,
                                       ShareAccess.Write,
                                       CreateDisposition.FILE_OPEN,
                                       CreateOptions.FILE_NON_DIRECTORY_FILE | CreateOptions.FILE_SYNCHRONOUS_IO_ALERT,
                                       null);

            if (_status == NTStatus.STATUS_SUCCESS)
            {
                _status = _store.SetFileInformation(fileHandle, new FileRenameInformationType2()
                {
                    FileName = targetFilePath
                });

                _status = _store.CloseFile(fileHandle);
            }

            return _status == NTStatus.STATUS_SUCCESS;
        }

答案1

得分: 1

  1. No, System.IO 不包含 SMB 1.0 或 SMB 2.0 客户端。
  2. 为了将文件从一个共享移动到另一个共享,客户端必须从源共享读取文件的内容,然后将其内容写入第二个共享。必须以这种方式复制它(然后从源共享中删除)。
英文:
  1. No, System.IO does not contain an SMB 1.0 or SMB 2.0 client.
  2. In order to move file from one share to another, the client must read the content of the file from the source share, and then write its content to the second share. it must be copied in this manner (and then deleted from the source).

答案2

得分: 0

// ** 不支持 SMB1. **

//使用 EzSmb;
//使用 System;
//使用 System.Threading.Tasks;

// 获取文件节点。
var file = await Node.GetNode(@"192.168.0.1\ShareName\FileName.txt", "用户名", "密码");

// 移动到子文件夹路径。
// ** 即使不改变文件/文件夹名称,也要写名称。 **
var movedFile = await file.Move(@"FolderName\RenamedFileName.txt");

Console.WriteLine(movedFile.FullPath);
// -> 192.168.0.1\ShareName\FolderName\RenamedFileName.txt


// 获取文件夹节点。
var folder = await Node.GetNode(@"192.168.0.1\ShareName\FolderName\SubFolderName", "用户名", "密码");

// 移动到上级路径。
var movedFolder = await folder.Move(@"..\RenamedSubFolderName");

Console.WriteLine(movedFolder.FullPath);
// -> 192.168.0.1\ShareName\RenamedSubFolderName
英文:

I found EzSmb <https://github.com/ume05rw/EzSmb> library with which you could move files within same SMB share using the following code:

// ** SMB1 NOT Supported. **

//using EzSmb;
//using System;
//using System.Threading.Tasks;

// Get Node of file.
var file = await Node.GetNode(@&quot;192.168.0.1\ShareName\FileName.txt&quot;, &quot;userName&quot;, &quot;password&quot;);

// Move to child folder path.
// ** Even if you don&#39;t change the file/folder name, write the name. **
var movedFile = await file.Move(@&quot;FolderName\RenamedFileName.txt&quot;);

Console.WriteLine(movedFile.FullPath);
// -&gt; 192.168.0.1\ShareName\FolderName\RenamedFileName.txt


// Get Node of folder.
var folder = await Node.GetNode(@&quot;192.168.0.1\ShareName\FolderName\SubFolderName&quot;, &quot;userName&quot;, &quot;password&quot;);

// Move to Parent path.
var movedFolder = await folder.Move(@&quot;..\RenamedSubFolderName&quot;);

Console.WriteLine(movedFolder.FullPath);
// -&gt; 192.168.0.1\ShareName\RenamedSubFolderName

huangapple
  • 本文由 发表于 2023年2月8日 18:58:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75384777.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定