You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用C#在Azure文件共享中复制文件?现有代码故障求助

Azure文件共享内文件复制故障排查与修复方案

问题分析与修复步骤

1. 补充错误处理与复制状态校验

原代码未捕获异常、未验证源文件存在性,也未跟踪复制进度,这会导致无法定位失败原因。以下是添加完整校验逻辑的修改版代码:

using System;
using System.IO;
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)
        {
            try
            {
                ShareFileClient sourceFile = new ShareFileClient(connectionstring, filesharename, Sourcepath);
                
                // 先验证源文件是否存在
                if (!sourceFile.Exists())
                {
                    Console.WriteLine("错误:源文件不存在");
                    return;
                }

                ShareFileClient destFile = new ShareFileClient(connectionstring, filesharename, DestPath);
                
                // 确保目标文件的父文件夹存在,否则复制会失败
                string destDirPath = Path.GetDirectoryName(DestPath).Replace("\\", "/");
                ShareDirectoryClient destDir = new ShareDirectoryClient(connectionstring, filesharename, destDirPath);
                destDir.CreateIfNotExists();

                // 启动复制并获取初始状态
                var copyInfo = destFile.StartCopy(sourceFile.Uri);
                
                // 轮询等待复制完成或失败
                while (true)
                {
                    var fileProps = destFile.GetProperties();
                    switch (fileProps.CopyStatus)
                    {
                        case CopyStatus.Success:
                            Console.WriteLine("文件复制完成");
                            return;
                        case CopyStatus.Failed:
                            Console.WriteLine($"复制失败:{fileProps.CopyStatusDescription}");
                            return;
                        default:
                            // 等待1秒后再次检查状态
                            System.Threading.Thread.Sleep(1000);
                            break;
                    }
                }
            }
            catch (RequestFailedException ex)
            {
                Console.WriteLine($"Azure服务错误:{ex.Message},错误码:{ex.Status}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"未知错误:{ex.Message}");
            }
        }
    }
}

2. 检查权限与连接字符串有效性

  • 确认连接字符串格式正确:需包含存储账户的AccountName和AccountKey,或使用具备**读(源文件)、写/创建(目标文件)**权限的SAS URI。
  • 验证存储账户权限:如果使用账户密钥,确保当前账户拥有Storage File Data Contributor角色;如果使用SAS,需在生成时勾选Read、Write、Create权限。

3. 修正路径格式

Azure文件共享路径需使用正斜杠/分隔(如source_folder/test.txt),避免使用反斜杠\,若代码中路径包含反斜杠,需提前替换为正斜杠。

4. 清理冗余依赖

代码中引用的Microsoft.Office.Interop.Excel未被使用,建议移除该引用,避免不必要的依赖冲突。

内容的提问来源于stack exchange,提问作者Anshul76

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 01:57:20