C #: System.Diagnostics.Process.Start ("Explorer.exe", @ "/ select" + FilePath). Cannot open file when file name is Unicode character

I want to open the file location using the Explorer window. I am using C # with code

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

it works well with a simple English character, but it cannot open the file location if the file name is a Unicode character (Thia language).

Can anybody help?

+6
source share
5 answers

Try putting it in quotation marks:

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

No problem with this piece of code:

  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, the file is created and displayed correctly in Explorer. You have not documented the version of your operating system to one of the failure modes, although very small. A much more likely problem is the file system mapped to your E: drive. Like FAT32 on a flash drive or network redirector. Ask questions about this, respectively, at superuser.com and serverfault.com. Remember to document these important details.

+2
source

The following code works for me with files with Korean characters (are Unicode characters). Try it and let me know if this 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); } } 
0
source

Explorer will go to the default folder in this case, My Documents, if the folder you are trying to open does not exist. Make sure it exists.

0
source

Here's the deal, as far as I defined: at least with Windows 8.1, “Explorer.exe” seems to highlight all character combinations before searching for the file. You can check this either in C # or in the console (first chcp 65001 to switch to Unicode mode). If you try to open a target named ปู (Thai for "crab"), this will not work, but if you remove the vowel, so that you only have , it will work. In addition, if you have a folder named ป and you, as you open ปู, open the folder ป!

This explains why some other developers have no problems; the problem is not ascii : rather, these are file names with compound characters. Not all languages ​​use them, and even in the languages ​​that do this, not all file names have them.

The good news is that there is another way to open them that do not have this problem, which is described by @ bert-huijben in this answer .

For completeness, here is a version similar to what I ended up using:

  [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); } } 
0
source

Source: https://habr.com/ru/post/896486/


All Articles