Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Can';t使用System.IO.File.Copy()将文件复制到卷Id_C#_.net_Winforms - Fatal编程技术网

C# Can';t使用System.IO.File.Copy()将文件复制到卷Id

C# Can';t使用System.IO.File.Copy()将文件复制到卷Id,c#,.net,winforms,C#,.net,Winforms,我有一个使用.Net Framework 4.5.2的C#WinForm应用程序。我正在获取一个卷ID列表,以便将文件复制到根文件夹 ManagementObjectSearcher ms = new ManagementObjectSearcher("Select * from Win32_Volume"); foreach(ManagementObject mo in ms.Get()) { var guid = mo["DeviceID"].ToString();

我有一个使用.Net Framework 4.5.2的C#WinForm应用程序。我正在获取一个卷ID列表,以便将文件复制到根文件夹

  ManagementObjectSearcher ms = new ManagementObjectSearcher("Select * from Win32_Volume");    
foreach(ManagementObject mo in ms.Get())   
{
    var guid = mo["DeviceID"].ToString();
}
然后我获取卷Id并调用System.IO.File.Copy()进行复制

但是获取“路径中的非法字符”

我知道这不是权限问题,我可以使用命令提示符中的“复制”将文件复制到卷Id。是否可以使用.Net Framework复制文件以使用卷Id而不是驱动器号复制文件


感谢查看Microsoft的参考源代码,如果如第页所述禁用
UseLegacyPathHandling
,您应该能够使其正常工作

配置示例:

<configuration>  
    <runtime>
        <AppContextSwitchOverrides value="Switch.System.IO.UseLegacyPathHandling=false" /> 
    </runtime> 
</configuration> 


什么框架版本?查看参考源,如果禁用
UseLegacyPathHandling
,您可能会逍遥法外。这里是检查的地方,框架版本是第一句话中的版本。我阅读失败,在我可以更新注释后识别它。你可能得把它加高一点。谢谢,它起作用了。我加了一句精彩的话。我补充了一个答案。
      at System.Security.Permissions.FileIOPermission.EmulateFileIOPermissionChecks(String fullPath)
   at System.Security.Permissions.FileIOPermission.QuickDemand(FileIOPermissionAccess access, String fullPath, Boolean checkForDuplicates, Boolean needFullPath)
   at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite, Boolean checkHost)
   at System.IO.File.Copy(String sourceFileName, String destFileName)
   at ThumbDrives.Form1.button1_Click(Object sender, EventArgs e) in Form1.cs:line 46
internal static void EmulateFileIOPermissionChecks(string fullPath)
{
    //...
    if (AppContextSwitches.UseLegacyPathHandling || !PathInternal.IsDevice(fullPath))
    {
        if (PathInternal.HasWildCardCharacters(fullPath))
        {
            throw new ArgumentException(Environment.GetResourceString("Argument_InvalidPathChars"));
        }

        //...
    }
}
<configuration>  
    <runtime>
        <AppContextSwitchOverrides value="Switch.System.IO.UseLegacyPathHandling=false" /> 
    </runtime> 
</configuration>