2011-09-02 21 views
6

我想用窗口資源管理器打開文件的位置。我使用C#代碼爲C#:System.Diagnostics.Process.Start(「Explorer.exe」,@「/ select」+ FilePath)。當文件名是unicode字符時無法打開文件

System.Diagnostics.Process.Start("Explorer.exe", @"/select," + FilePath) 

它用簡單的英文字符效果很好,但如果該文件的名稱是Unicode字符(硫雜語)它無法打開該文件的位置。

任何人都可以幫忙嗎?

+0

我已經確定問題是與由與輔音文字泰國元音的話。例如。 ภาษาไทย(「泰語」)正常工作,但ป((螃蟹)失敗。 unicode規範化形式明顯不同;我的理解是Windows NFC和NFD格式的文件名不相等。但我還沒有弄清楚如何解決這個問題。只是在參數上調用''''mystring.Normalize(...)''''沒有幫助。 –

回答

3

嘗試把它放在引號:

System.Diagnostics.Process.Start("Explorer.exe", @"/select,""" + FilePath + "\"") 
+0

對不起,它不起作用。它會打開文件夾我的文檔。 –

+0

@YongKuy:請在此處張貼你正在使用的路徑 –

+0

「E:\\ Test \\ស្រលាញ់សង្សារគេ.DAT」 –

2

不麻煩,此代碼片段:

static void Main(string[] args) { 
     string path = @"c:\temp\លួចស្រលាញ់សង្សារគេ.DAT"; 
     System.IO.File.WriteAllText(path, "hello"); 
     string txt = System.IO.File.ReadAllText(path); 
    } 

Windows 7中,在資源管理器中創建並顯示文件正確。您沒有記錄您的操作系統版本,因此這是一種失敗模式,儘管它很小。映射到E:驅動器的文件系統出現問題的可能性更大。像閃存驅動器或網絡重定向器上的FAT32卷一樣。請分別在superuser.com和serverfault.com上提問。不要忘記記錄這些重要細節。

0

以下代碼適用於帶有韓文字符(是unicode字符)的文件。請嘗試一下,讓我知道它是否有效。

 ... 
     if (this.IsDirectory()) 
     { 
      OpenFileWith("explorer.exe", this.FullPath, "/root,"); 
     } 
     else 
     { 
      OpenFileWith("explorer.exe", this.FullPath, "/select,"); 
     } 
     ... 

    public static void OpenFileWith(string exePath, string path, string arguments) 
    { 
     if (path == null) 
      return; 

     try 
     { 
      System.Diagnostics.Process process = new System.Diagnostics.Process(); 
      process.StartInfo.WorkingDirectory = Path.GetDirectoryName(path); 
      if (exePath != null) 
      { 
       process.StartInfo.FileName = exePath; 
       //Pre-post insert quotes for fileNames with spaces. 
       process.StartInfo.Arguments = string.Format("{0}\"{1}\"", arguments, path); 
      } 
      else 
      { 
       process.StartInfo.FileName = path; 
       process.StartInfo.WorkingDirectory = Path.GetDirectoryName(path); 
      } 
      if (!path.Equals(process.StartInfo.WorkingDirectory)) 
      { 
       process.Start(); 
      } 
     } 
     catch(System.ComponentModel.Win32Exception ex) 
     { 
      FormManager.DisplayException(ex, MessageBoxIcon.Information); 
     } 
    } 
+0

它不爲我工作,但無論如何,感謝 –

0

如果您嘗試打開的文件夾不存在,Explorer將轉到默認文件夾'My Documents'。確保它存在。

+0

爲什麼downvote? –

0

就我所知:至少從Windows 8.1開始,「Explorer.exe」似乎在查找文件之前刪除所有組合字符。您可以在c#或控制檯中進行測試(首先進行chcp 65001以獲取unicode模式)。如果您嘗試打開一個名爲ปtarget的目標(泰語爲「螃蟹」),它將無法工作,但如果您刪除下面的元音標記以使您剛剛出現,它就會起作用。此外,如果您有一個名爲「ป」的文件夾,並且您打開ปู,它將打開「ป」文件夾!

這解釋了爲什麼一些其他開發者沒有問題;該問題不是非ascii:相反,它是具有可組合字符的文件名。並非所有的語言都使用它們,即使是在使用它們的語言中,也不是所有的語言都有它們。

好消息是,有一種不同的方式可以打開這些沒有這個問題的方法,這是由@ bert-huijben在this answer中描述的

爲了完整起見,這裏的相似,我結束了使用的版本:

[DllImport("shell32.dll", ExactSpelling = true)] 
    public static extern void ILFree(IntPtr pidlList); 

    [DllImport("shell32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)] 
    public static extern IntPtr ILCreateFromPathW(string pszPath); 

    [DllImport("shell32.dll", ExactSpelling = true)] 
    public static extern int SHOpenFolderAndSelectItems(IntPtr pidlList, uint cild, IntPtr children, uint dwFlags); 

    public void SelectItemInExplorer(string path) 
    { 
     var pidlList = ILCreateFromPathW(path); 
     if(pidlList == IntPtr.Zero) 
      throw new Exception(string.Format("ILCreateFromPathW({0}) failed",path)); 
     try 
     { 
      Marshal.ThrowExceptionForHR(SHOpenFolderAndSelectItems(pidlList, 0, IntPtr.Zero, 0)); 
     } 
     finally 
     { 
      ILFree(pidlList); 
     } 
    }