ume05rw / EzSmb

SMB(Windows shared folder) Clinet Library powered by TalAloni's SmbLibrary, Xamarin & .NET Core Ready.
GNU Lesser General Public License v3.0
53 stars 20 forks source link

Not able to move files #12

Closed felipeatsix closed 2 years ago

felipeatsix commented 2 years ago

Hi. First, thank you very much for this great library, it is very simple to use and it is a time saver.

Context

I'm trying to move files from UNC to UNC in a windows domain, running my application from a docker Linux container.

Testing environment

For testing this I have created the following file share on my local host

Then I Added 2 .txt files in \\localhost\src

Succeeded

My container was able to read the .txt files from the src folder with the following code

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            var file = await Node.GetNode(@"<ipaddress>\src", "<username>", "<password>");
            var nodes = await file.GetList();
            foreach (var node in nodes)
            {
                Console.WriteLine(node.Name);
            }            
        }

Failed

Although, when trying to use the Move method, I don't get any exceptions but the files are not moved as expected

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            var folder = await Node.GetNode(@"<ipaddress>\src", "<username>", "<password>");
            var nodes = await folder.GetList();
            foreach (var node in nodes)
            {
                _logger.LogInformation($"Moving file {node.Name}");
                await node.Move($@"<ipaddress>\dst\{node.Name}");
            }            
        }

Am I doing something wrong? should this code expect to move files from src to dst file share?

ume05rw commented 2 years ago

The name of the method may have been misleading. I've not implemented the ability to move files to another server.

The first argument of the Move method is a relative path, like: "... \anotherFolder\file.txt"

To move a file to another server, use the Read method to get the MemoryStream, Write on the folder node of the other server, and Delete the original node.

felipeatsix commented 2 years ago

Thank you very much for the quick reply, I'm going to try it once I have some time to get back to it, as soon as I can confirm I can do the needful I'll be back to close this.

felipeatsix commented 2 years ago

This worked! thank you

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {

            if (stoppingToken.IsCancellationRequested)
            {
                return;
            }

            var source = await Node.GetNode(@"<ip>\src", "<user>", "<password>");
            var sourceFiles = await source.GetList();

            var destination = await Node.GetNode(@"<ip>\dst", "<user>", "<password>");

            foreach (var file in sourceFiles)
            {
                var stream = await file.Read();
                await destination.Write(stream, file.Name);
                await file.Delete();
            }
            await Task.Delay(5000);
        }