.net 程序集::GetExecutionGassembly()->;位置现在抛出ArgumentException:路径中的非法字符
在Visual Studio 2010(和.Net 4)中的node.js本机插件中,我可以使用.net 程序集::GetExecutionGassembly()->;位置现在抛出ArgumentException:路径中的非法字符,.net,node.js,visual-studio-2015,c++-cli,.net,Node.js,Visual Studio 2015,C++ Cli,在Visual Studio 2010(和.Net 4)中的node.js本机插件中,我可以使用 System::Reflection::Assembly::GetExecutingAssembly()->Location 要获取正在运行的C++/CLI程序集的路径,但在Visual Studio 2015项目(和.Net 4.6)中的node.js加载项中,我遇到一个异常: System.ArgumentException: Illegal characters in path.
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-25
Visual Studio 2017仍未解决这一问题,这次尝试.Net 4.5.2虽然
Location
属性抛出,但仍然可以使用CodeBase
,返回类似以下内容:
file://?/C:/.../my-addon.node
这就解释了非法字符“?”
一个新Uri(Application.CodeBase).LocalPath将返回
/?/C:/.../my-addon.node
C:\...\my-addon.node
因此,getExecutionGassembly()->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
C:\...\my-addon.node