Search code examples
c#.netazurefileshare

How can I copy one file from one folder to another within an Azure Fileshare using C#?


 using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
 using System.Runtime.InteropServices;
 using System.Text;
 using System.Threading.Tasks;
 using Excel = Microsoft.Office.Interop.Excel;
 using Azure;
 using Azure.Storage.Files.Shares;
 using Azure.Storage.Files.Shares.Models;
 
 namespace SpeakerDocumentsMigration
 {
     class Program
     {
        static string connectionstring = "";
        static string filesharename = "";
        static string Sourcepath = ""; 
        static string DestPath = "";
        
        static void Main(string[] args)
        {
            ShareFileClient sourcefile = new ShareFileClient(connectionstring, filesharename,Sourcepath);
 
            ShareFileClient destfile = new ShareFileClient(connectionstring, filesharename, DestPath);
 
            destfile.StartCopy(sourcefile.Uri);    

        }
    }
}

Seems like above code is not working, assuming I enter values for all the variables declared.

Can someone please guide how can I proceed?


Solution

  • I tried the below code and can copy one file from one folder to another within one Azure Fileshare using C# dot net.

    Code :

    using Azure.Storage.Files.Shares;
    using Azure.Storage.Files.Shares.Models;
    using System;
    using System.IO;
    using System.Threading.Tasks;
    
    namespace SpeakerDocumentsMigration
    {
        class Program
        {
            static string connectionstring = "<storage_connec_string>";
            static string filesharename = "<fileshare_name>";
            static string SourcePath = "<sourcefolder_name/sourcefile_name.txt>"; 
            static string DestPath = "<destinationfolder_name/destinationfile_name.txt>"; 
    
            static async Task Main(string[] args)
            {
                ShareClient shareClient = new ShareClient(connectionstring, filesharename);
                ShareDirectoryClient sourceDirectory = shareClient.GetDirectoryClient(Path.GetDirectoryName(SourcePath));
                ShareDirectoryClient destDirectory = shareClient.GetDirectoryClient(Path.GetDirectoryName(DestPath));
    
                if (await sourceDirectory.ExistsAsync() && await destDirectory.ExistsAsync())
                {
                    ShareFileClient sourceFile = sourceDirectory.GetFileClient(Path.GetFileName(SourcePath));
                    ShareFileClient destFile = destDirectory.GetFileClient(Path.GetFileName(DestPath));
    
                    if (await sourceFile.ExistsAsync())
                    {
                        ShareFileProperties properties = await sourceFile.GetPropertiesAsync();
                        if (properties.CopyStatus == CopyStatus.Success)
                        {
                            Console.WriteLine("File already copied.");
                        }
                        else
                        {
                            await destFile.StartCopyAsync(sourceFile.Uri);
    
                            await WaitForCopyToCompleteAsync(destFile);
    
                            Console.WriteLine("File copied successfully.");
                        }
                    }
                    else
                    {
                        Console.WriteLine("Source file does not exist.");
                    }
                }
                else
                {
                    Console.WriteLine("Source or destination folder does not exist.");
                }
            }
    
            static async Task WaitForCopyToCompleteAsync(ShareFileClient fileClient)
            {
                ShareFileProperties properties = await fileClient.GetPropertiesAsync();
                while (properties.CopyStatus == CopyStatus.Pending)
                {
                    await Task.Delay(1000); 
                    properties = await fileClient.GetPropertiesAsync();
                }
            }
        }
    }
    

    Output :

    It runs successfully and copies one file from one folder to another in Fileshare.

    enter image description here

    I got the copied data from one file from one folder to another in file share at the Azure portal as below.

    enter image description here

    Source folder file :

    enter image description here

    Destination folder file : enter image description here