使用以管理员身份运行的可执行文件的完整路径时,Windows 10无法访问指定的设备,路径或文件

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:

  • 我以管理员身份运行Windows
  • 用户可以访问该文件(为计算机中的每个人启用安全属性).
  • c:\b.exe 工作!!
  • 我不是在寻找像更改应用程序工作目录和运行Process.Start("b.exe")之类的解决方案.

谢谢你们,

编辑:

  • 指定的路径不是错误的,文件不会丢失.
  • 计算机的每个用户作为总控制级别加入文件夹和文件(a目录和b.exe)
  • 我把exe放在根目录下时工作:"c:\ b.exe"
  • 与.bat文件相同的行为只有"echo Hello inside"
  • 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)

Pab*_*ney 4

我找到了 !!

\n\n

这是从 Windows 10 资源管理器中的某些标签复制/粘贴时在文件路径中添加的一些额外的不可打印字节。

\n\n

考虑这段代码:

\n\n
Console.WriteLine(new DirectoryInfo(@"c:\\a\\"));\nConsole.WriteLine(new DirectoryInfo(@"\xe2\x80\xaac:\\a\\"));  \n
Run Code Online (Sandbox Code Playgroud)\n\n

这些行看起来相同,不应该引发任何异常(即使目录c:\\a不存在),但实际上,如果您在应用程序中复制/粘贴上面的代码,第二行将引发以下NotSupportedException单词:“给定的路径”不支持格式”。

\n\n

我最终检查了 .NET 源代码,发现了引发 NotSupportedException 的方法StringExpressionSet.Canonicalize :

\n\n
...\n if (path.IndexOf( \':\', 2 ) != -1)\n      throw new NotSupportedException( Environment.GetResourceString( "Argument_PathFormatNotSupported" ) );\n...\n
Run Code Online (Sandbox Code Playgroud)\n\n

实际上:

\n\n
Console.WriteLine(@"c:\\a\\".IndexOf( \':\', 2 )); //  results -1\nConsole.WriteLine(@"\xe2\x80\xaac:\\a\\".IndexOf( \':\', 2 )); //  result 2\n// Copy/Paste to test\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n\n

我在哪里抓到的?

\n\n

为了不犯任何打字错误,我习惯将目录路径从右键单击复制到文件 -> - Properties>Security

\n\n

在此输入图像描述

\n\n

现在你被警告了

\n