gpt4 book ai didi

.net - Assembly::GetExecutingAssembly()->Location 现在抛出 ArgumentException:路径中存在非法字符

转载 作者:太空宇宙 更新时间:2023-11-03 22:29:40 27 4
gpt4 key购买 nike

在 Visual Studio 2010(和 .Net 4)中的 Node.js native 插件中,我可以使用

System::Reflection::Assembly::GetExecutingAssembly()->Location

获取正在运行的 C++/CLI 程序集的路径,但在 Visual Studio 2015 项目(和 .Net 4.6)的 Node.js 插件中,出现异常:

System.ArgumentException: Illegal characters in path.
at System.IO.Path.CheckInvalidPathChars(String path, Boolean checkAdditional)
at System.Security.Permissions.FileIOPermission.CheckIllegalCharacters(
String[] str)
at System.Security.Permissions.FileIOPermission.AddPathList(
FileIOPermissionAccess access, AccessControlActions control,
String[] pathListOrig, Boolean checkForDuplicates,
Boolean needFullPath, Boolean copyPathList)
at System.Security.Permissions.FileIOPermission..ctor(
FileIOPermissionAccess access, String path)
at System.Reflection.RuntimeAssembly.get_Location()

知道如何获取正在运行的程序集的路径吗?

编辑2018-01-25Visual Studio 2017 仍未解决此问题,这次尝试 .Net 4.5.2

最佳答案

虽然 Location 属性抛出异常,但仍然可以使用 CodeBase,它返回如下内容:

file://?/C:/.../my-addon.node

这可以解释非法字符:“?”

将返回一个新的 Uri(Application.CodeBase).LocalPath

/?/C:/.../my-addon.node

因此 GetExecutingAssembly()->Location 的解决方法可能是:

String^ GetExecutingAssemblyLocation()
{
try
{
return Path::GetDirectoryName(Assembly::GetExecutingAssembly()->Location);
}
catch (Exception^ e)
{
String^ codeBase = Assembly::GetExecutingAssembly()->CodeBase;
if (codeBase->StartsWith("file://?/"))
{
Uri^ codeBaseUri = gcnew Uri("file://" + codeBase->Substring(8));
return codeBaseUri->LocalPath;
}
else
throw;
}
}

将会返回

C:\...\my-addon.node

关于.net - Assembly::GetExecutingAssembly()->Location 现在抛出 ArgumentException:路径中存在非法字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39263610/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com