找不到网络名。 (Exception from HRESULT: 0x80070043)

最近在客户电脑出现了一个怪异的报错,C# 打开保存文件对话框报如下错误:

找不到网络名。 (Exception from HRESULT: 0x80070043) at MS.Internal.AppModel.ShellUtil.GetShellItemForPath(String path)
at Microsoft.Win32.FileDialog.PrepareVistaDialog(IFileDialog dialog)
at Microsoft.Win32.FileDialog.RunVistaDialog(IntPtr hwndOwner)
at Microsoft.Win32.CommonDialog.ShowDialog()

是因为SaveFileDialog的InitialDirectory属性设置了相对路径导致的。

经分析是客户在Mac电脑上,通过Parallels安装Win11虚拟机,导致访问桌面、我的文档等文件夹的时候,路径是:\\Mac\User\MyDocument,正常在Windows下的路径是:C:\Users\jinqi\Documents,为什么会这样呢,是因为这个平台创建的虚拟机将Mac的常用文件夹和虚拟机内的常用文件夹共享了,参考连接

处理办法是在设置InitialDirectory的时候,判断路径是否为网络路径,如果是,就不设置值就行了。

代码如下:

SaveFileDialog dialog = new SaveFileDialog();
string defaultPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
//mac虚拟机,win11的时候,桌面文件夹是:\\Mac\User\Desktop,这时候用InitialDirectory会异常
if (!defaultPath.StartsWith("\\"))
{
  dialog.InitialDirectory = defaultPath;
}

原文地址:https://www.cnblogs.com/JqkAman/p/15693814.html