找不到網絡名。 (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;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM