C#: System.Diagnostics.Process.Start(“Explorer.exe”, @“/select” + FilePath). Can not open file when file's name is unicode character

前端 未结 5 1627
深忆病人
深忆病人 2020-12-11 05:31

I want to open file\'s location with window Explorer. I am using C# with code

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


        
5条回答
  •  春和景丽
    2020-12-11 06:01

    The following code works for me with files with korean characters (are unicode characters). Please try it and let me know if it works.

           ...
           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);
            }
        }
    

提交回复
热议问题