如何通过编程方式打开Azure Storage Explorer至指定子文件夹?
实现Azure Storage Explorer直接定位到指定子文件夹
我可以用以下代码流畅打开Windows文件资源管理器:
var psi = new System.Diagnostics.ProcessStartInfo() { FileName = startPath, UseShellExecute = true }; System.Diagnostics.Process.Start(psi);
现在希望给Azure Storage Explorer实现类似功能,尝试了以下代码:
var arguments = $"--account-name mystorage --container images --path mysubfolder"; var userDirectory = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); // 获取Azure Storage Explorer的可执行文件路径 var workingDirectory = userDirectory+"\\Programs\\Microsoft Azure Storage Explorer"; var fullExeFileName = workingDirectory+"\\StorageExplorer.exe"; if (!Directory.Exists(workingDirectory)) { throw new DirectoryNotFoundException(workingDirectory); } if (!File.Exists(fullExeFileName)) { throw new FileNotFoundException(fullExeFileName); } var processStartInfo = new ProcessStartInfo { FileName = fullExeFileName, Arguments = arguments, UseShellExecute = true, WorkingDirectory = workingDirectory }; Process.Start(processStartInfo);
这段代码能打开Storage Explorer,但无法定位到目标子文件夹。我试过用浏览器打开直接链接,但会弹出对话框,不符合需求;也在Visual Studio命令提示符中测试参数,同样无效。
执行命令:
storageexplorer "--account-name mystorage"
生成的日志如下:
[2025-01-26T22:15:40.815Z] (main:17928) <NONE> 日志级别: info [2025-01-26T22:15:40.815Z] (main:17928) <INFO> 启动应用状态: { lastWindowState: { rect: { x: 900, y: 652, width: 1760, height: 902 }, maximized: false } } [2025-01-26T22:15:40.907Z] (main:17928) <INFO> 从quit调用handleQuitOrRelaunch [2025-01-26T22:15:40.907Z] (main:17928) <INFO> 正在退出,sessionEnded: false, force: true, quitOrRelaunch: quit [2025-01-26T22:15:40.907Z] (main:17928) <ERRO> 尝试退出时发生错误: {} [2025-01-26T22:15:40.908Z] (main:17928) <INFO> 调用quit,再见!
解决方案
问题出在参数传递格式上,Azure Storage Explorer不支持零散键值对形式的启动参数,需要使用官方支持的深度链接URI来直接定位资源。
修改后的代码如下:
var accountName = "mystorage"; var containerName = "images"; var folderPath = "mysubfolder"; // 构造符合要求的深度链接 var targetUri = $"storageexplorer://v1/explore/storage/account/{accountName}/blobContainer/{containerName}/path/{folderPath}"; var userDirectory = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); var workingDirectory = Path.Combine(userDirectory, "Programs", "Microsoft Azure Storage Explorer"); var fullExeFileName = Path.Combine(workingDirectory, "StorageExplorer.exe"); if (!Directory.Exists(workingDirectory)) { throw new DirectoryNotFoundException(workingDirectory); } if (!File.Exists(fullExeFileName)) { throw new FileNotFoundException(fullExeFileName); } var processStartInfo = new ProcessStartInfo { FileName = fullExeFileName, Arguments = $"\"{targetUri}\"", // URI参数需用引号包裹,避免特殊字符解析错误 UseShellExecute = true, WorkingDirectory = workingDirectory }; Process.Start(processStartInfo);
关键说明
- 必须使用
storageexplorer://协议的深度链接,这是Azure Storage Explorer官方支持的跳转方式 - URI参数需要用双引号包裹,防止路径中的特殊字符(如空格)导致参数解析失败
- 若目标是多级子文件夹,直接在
path/后拼接完整路径即可,例如path/folder1/subfolder2
内容的提问来源于stack exchange,提问作者Kirsten
相关产品推荐
相关产品推荐

