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

Azure文件存储:文件上传下载及列表数据网格展示技术问询

嘿,我来帮你搞定Azure文件存储的上传、下载操作,还有把文件列表展示到DataGridView里——刚好我之前做过类似的需求,给你一步步拆解:

一、前期准备

首先得确保你做好这几件事:

  • 安装NuGet包:Azure.Storage.Files.Shares(这是官方的Azure文件存储SDK,用它操作最稳定靠谱)
  • 拿到你的Azure存储账户连接字符串:在Azure Portal里找到对应的存储账户,进入「访问密钥」页面就能复制到。
  • 提前创建好目标文件共享(如果还没的话),也可以用代码自动创建。

二、上传文件到Azure文件存储

我用C#举例子(毕竟DataGridView是.NET控件),代码逻辑很直观:

using Azure.Storage.Files.Shares;
using System.IO;

// 替换成你的实际配置
string connectionString = "你的存储账户连接字符串";
string shareName = "你的文件共享名称";
string localFilePath = @"C:\本地文件路径\test.pdf";
string remoteDirectory = "目标子目录(可选,根目录留空)";
string remoteFileName = "上传后的文件名.pdf";

try
{
    // 初始化文件共享客户端
    ShareClient shareClient = new ShareClient(connectionString, shareName);
    await shareClient.CreateIfNotExistsAsync(); // 共享不存在就自动创建

    // 获取目标目录客户端
    ShareDirectoryClient directoryClient = shareClient.GetDirectoryClient(remoteDirectory);
    await directoryClient.CreateIfNotExistsAsync(); // 目录不存在也自动创建

    // 上传文件
    ShareFileClient fileClient = directoryClient.GetFileClient(remoteFileName);
    using FileStream stream = File.OpenRead(localFilePath);
    await fileClient.UploadAsync(stream, overwrite: true); // overwrite设为true会覆盖同名文件

    MessageBox.Show("文件上传成功!");
}
catch (Exception ex)
{
    MessageBox.Show($"上传失败:{ex.Message}");
}

小提示:如果不需要指定子目录,直接用shareClient.GetFileClient(remoteFileName)就行,跳过目录那一步。

三、从Azure文件存储下载文件

下载逻辑和上传反过来,代码示例:

string connectionString = "你的存储账户连接字符串";
string shareName = "你的文件共享名称";
string remoteFilePath = "目标文件路径(比如:子目录/文件名.pdf)";
string localSavePath = @"C:\保存路径\downloaded.pdf";

try
{
    ShareClient shareClient = new ShareClient(connectionString, shareName);
    if (!await shareClient.ExistsAsync())
    {
        MessageBox.Show("文件共享不存在!");
        return;
    }

    // 获取要下载的文件客户端
    ShareFileClient fileClient = shareClient.GetFileClient(remoteFilePath);
    if (!await fileClient.ExistsAsync())
    {
        MessageBox.Show("目标文件不存在!");
        return;
    }

    // 下载到本地
    using FileStream stream = File.Create(localSavePath);
    await fileClient.DownloadToAsync(stream);

    MessageBox.Show("文件下载成功!");
}
catch (Exception ex)
{
    MessageBox.Show($"下载失败:{ex.Message}");
}

四、获取文件列表并绑定到DataGridView

这一步需要先把文件的关键信息(名称、大小、创建时间等)收集到集合里,再绑定到DataGridView。注意一定要在UI线程更新控件哦!

第一步:创建文件信息类(用来存文件属性)

public class AzureFileInfo
{
    public string FileName { get; set; }
    public long FileSizeBytes { get; set; }
    public DateTimeOffset? CreatedTime { get; set; }
    public DateTimeOffset? LastModifiedTime { get; set; }
}

第二步:获取列表并绑定到DataGridView

using System.Windows.Forms; // WinForms环境下需要引用

private async void LoadFilesToDataGridView()
{
    string connectionString = "你的存储账户连接字符串";
    string shareName = "你的文件共享名称";
    string targetDirectory = "要遍历的目录(根目录留空)";

    try
    {
        ShareClient shareClient = new ShareClient(connectionString, shareName);
        if (!await shareClient.ExistsAsync())
        {
            MessageBox.Show("文件共享不存在!");
            return;
        }

        ShareDirectoryClient directoryClient = shareClient.GetDirectoryClient(targetDirectory);
        var fileInfos = new List<AzureFileInfo>();

        // 遍历目录下的所有文件(跳过子目录,需要递归的话自己加逻辑)
        await foreach (var item in directoryClient.GetFilesAndDirectoriesAsync())
        {
            if (item.IsFile)
            {
                ShareFileClient fileClient = directoryClient.GetFileClient(item.Name);
                var properties = await fileClient.GetPropertiesAsync();

                fileInfos.Add(new AzureFileInfo
                {
                    FileName = item.Name,
                    FileSizeBytes = properties.ContentLength,
                    CreatedTime = properties.CreatedOn,
                    LastModifiedTime = properties.LastModified
                });
            }
        }

        // 在UI线程更新DataGridView
        dataGridView1.Invoke((MethodInvoker)delegate
        {
            dataGridView1.DataSource = fileInfos;
            // 给列设置友好的显示名称
            dataGridView1.Columns["FileSizeBytes"].HeaderText = "文件大小(字节)";
            dataGridView1.Columns["CreatedTime"].HeaderText = "创建时间";
            dataGridView1.Columns["LastModifiedTime"].HeaderText = "最后修改时间";
        });
    }
    catch (Exception ex)
    {
        MessageBox.Show($"加载文件列表失败:{ex.Message}");
    }
}

提示:如果要遍历所有子目录,需要写递归方法,遇到item.IsDirectory时,再调用GetFilesAndDirectoriesAsync遍历子目录即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 09:14:00