---
title: "How can I copy one file from one folder to another within an Azure Fileshare using C#?"  
description: "How can I copy one file from one folder to another within an Azure Fileshare using C#?"  
author: "Steilla Mitchel"  
published: 2023-09-19  
updated: 2023-09-25  
canonical: https://www.mindstick.com/forum/159900/how-can-i-copy-one-file-from-one-folder-to-another-within-an-azure-fileshare-using-c-sharp  
category: "c#"  
tags: ["c#", "file"]  
reading_time: 2 minutes  

---

# How can I copy one file from one folder to another within an Azure Fileshare using C#?

How can I [copy](https://www.mindstick.com/forum/161993/explain-the-numpy-array-copy-vs-view-with-example) one [file](https://www.mindstick.com/articles/59/encrypting-and-decrypting-files-using-c-sharp) from one folder to another within an [Azure](https://www.mindstick.com/articles/12586/microsoft-unveils-real-time-ai-for-azure) Fileshare using C#?

## Replies

### Reply by Aryan Kumar

To copy a file from one folder to another within an Azure File Share using C#, you can use the Azure.Storage.Files.Shares library, which provides Azure File Share storage functionality. Here's a step-by-step guide on how to do this:

1. Install the Azure.Storage.Files.Shares NuGet package if you haven't already. You can do this using the NuGet Package Manager Console in Visual Studio or through the Visual Studio Package Manager:

```plaintext
Install-Package Azure.Storage.Files.Shares -Version 12.10.0
```

2. Use the following C# code to copy a file from one folder to another within an Azure File Share:

```plaintext
using Azure.Storage.Files.Shares;
using Azure.Storage.Files.Shares.Models;
using System;
using System.Threading.Tasks;
class Program
{
   static async Task Main(string[] args)
   {
       string connectionString = "<your_connection_string>";
       string shareName = "<your_share_name>";
       string sourceDirectory = "source_folder"; // Replace with your source folder
       string destinationDirectory = "destination_folder"; // Replace with your destination folder
       string fileName = "your_file.txt"; // Replace with the name of the file you want to copy
       ShareServiceClient serviceClient = new ShareServiceClient(connectionString);
       ShareClient shareClient = serviceClient.GetShareClient(shareName);
       ShareDirectoryClient sourceDirectoryClient = shareClient.GetDirectoryClient(sourceDirectory);
       ShareDirectoryClient destinationDirectoryClient = shareClient.GetDirectoryClient(destinationDirectory);
       ShareFileClient sourceFileClient = sourceDirectoryClient.GetFileClient(fileName);
       ShareFileClient destinationFileClient = destinationDirectoryClient.GetFileClient(fileName);
       try
       {
           // Check if the source file exists
           if (await sourceFileClient.ExistsAsync())
           {
               // Start the copy operation
               CopyFileResult copyResult = await destinationFileClient.StartCopyAsync(sourceFileClient.Uri);
               // Wait for the copy operation to complete
               await WaitForCopyCompletion(destinationFileClient, copyResult.CopyId);
               Console.WriteLine("File copy completed successfully.");
           }
           else
           {
               Console.WriteLine("Source file does not exist.");
           }
       }
       catch (RequestFailedException ex)
       {
           Console.WriteLine($"Error copying file: {ex.Message}");
       }
   }
   static async Task WaitForCopyCompletion(ShareFileClient fileClient, string copyId)
   {
       do
       {
           CopyStatus copyStatus = (await fileClient.GetPropertiesAsync()).Value.CopyStatus;
           if (copyStatus == CopyStatus.Success)
           {
               return;
           }
           else if (copyStatus == CopyStatus.Aborted || copyStatus == CopyStatus.Failed)
           {
               throw new Exception("File copy operation failed.");
           }
           await Task.Delay(TimeSpan.FromSeconds(1));
       }
       while (true);
   }
}
```

Make sure to replace `**<your_connection_string>**`, `**<your_share_name>**`, `**source_folder**`, `**destination_folder**`, and `**your_file.txt**` with your actual Azure File Share details and file information.

This code establishes a connection to your Azure File Share, checks if the source file exists, and then initiates the copy operation using `**StartCopyAsync**`. It then waits for the copy to complete using the `**WaitForCopyCompletion**` method before notifying you of the result.


---

Original Source: https://www.mindstick.com/forum/159900/how-can-i-copy-one-file-from-one-folder-to-another-within-an-azure-fileshare-using-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
