Pab*_*ney 8 c# windows filesystems encoding windows-10
注意:我用a和exe 替换目录,b.exe我重复我做的每一个测试,以确保它不是一个输入语法.
我有一个非常简单的部分代码,从Windows XP到Windows 7完美运行.
var processPath = @"c:\a\b.exe" ; // this exe exists on my computer
Process.Start(processPath);
Run Code Online (Sandbox Code Playgroud)
并且
Directory.Exists(@"c:\a\") returns false on Windows 10.
Run Code Online (Sandbox Code Playgroud)
由于Windows 10(我尚未测试8和8.1),第一个代码将抛出System.ComponentModel.Win32Exception("未找到指定文件"),第二个将返回false..
我也注意到当我使用Windows运行窗口(Windows Key + R)运行"c:\ a\b.exe"时,它的行为也是一样的.
有没有解决这个问题的解决方法?优先考虑的是一种不需要重新编译的解决方案.
NB:
c:\b.exe 工作!!谢谢你们,
编辑:
a目录和b.exe)Console.WriteLine(String.Join("\r\n", Directory.GetDirectories(@"c:\"))) 显示目录 c:\a更新:
结果icalcs c:\a\:
c:\a\ Tout le monde:(OI)(CI)(F)
BUILTIN\Administrateurs:(I)(OI)(CI)(F)
AUTORITE NT\SystŠme:(I)(OI)(CI)(F)
BUILTIN\Utilisateurs:(I)(OI)(CI)(RX)
AUTORITE NT\Utilisateurs authentifi‚s:(I)(M)
AUTORITE NT\Utilisateurs authentifi‚s:(I)(OI)(CI)(IO)(M)
Run Code Online (Sandbox Code Playgroud)
结果icalcs c:\a\b.exe:
c:\a\b.exe Tout le monde:(I)(F)
BUILTIN\Administrateurs:(I)(F)
AUTORITE NT\SystŠme:(I)(F)
BUILTIN\Utilisateurs:(I)(RX)
AUTORITE NT\Utilisateurs authentifi‚s:(I)(M)
Run Code Online (Sandbox Code Playgroud)
"Tout le monde"意味着每个人.
更新:
在最后的新闻中,我能做到:
File.WriteAllBytes(@"c:\a\b.exe", somebinaries) ;
Run Code Online (Sandbox Code Playgroud)
但我无法做到
FileInfo fileInfo = new FileInfo(@"c:\a\b.exe") ;
Run Code Online (Sandbox Code Playgroud)
抛出'System.NotSupportedException'.StackTrace如下:
à System.Security.Permissions.FileIOPermission.QuickDemand(FileIOPermissionAccess access, String fullPath, Boolean checkForDuplicates, Boolean needFullPath)
à System.IO.FileInfo.Init(String fileName, Boolean checkHost)
à System.IO.FileInfo..ctor(String fileName)
à ConsoleApplication1.Program.Main(String[] args) dans F:\MAPW10\Development\Sources\Tools\ConsoleApplication1\Program.cs:ligne 22
à System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
à System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
à Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
à System.Threading.ThreadHelper.ThreadStart_Context(Object state)
à System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
à System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
à System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
à System.Threading.ThreadHelper.ThreadStart()
Run Code Online (Sandbox Code Playgroud)
这是从 Windows 10 资源管理器中的某些标签复制/粘贴时在文件路径中添加的一些额外的不可打印字节。
\n\n考虑这段代码:
\n\nConsole.WriteLine(new DirectoryInfo(@"c:\\a\\"));\nConsole.WriteLine(new DirectoryInfo(@"\xe2\x80\xaac:\\a\\")); \nRun Code Online (Sandbox Code Playgroud)\n\n这些行看起来相同,不应该引发任何异常(即使目录c:\\a不存在),但实际上,如果您在应用程序中复制/粘贴上面的代码,第二行将引发以下NotSupportedException单词:“给定的路径”不支持格式”。
我最终检查了 .NET 源代码,发现了引发 NotSupportedException 的方法StringExpressionSet.Canonicalize :
\n\n...\n if (path.IndexOf( \':\', 2 ) != -1)\n throw new NotSupportedException( Environment.GetResourceString( "Argument_PathFormatNotSupported" ) );\n...\nRun Code Online (Sandbox Code Playgroud)\n\n实际上:
\n\nConsole.WriteLine(@"c:\\a\\".IndexOf( \':\', 2 )); // results -1\nConsole.WriteLine(@"\xe2\x80\xaac:\\a\\".IndexOf( \':\', 2 )); // result 2\n// Copy/Paste to test\nRun Code Online (Sandbox Code Playgroud)\n\n为了不犯任何打字错误,我习惯将目录路径从右键单击复制到文件 -> - Properties>Security
现在你被警告了!
\n