[英]Powershell load custom assembly config file
I have a DLL file, created in visual studio from my c# code. 我有一个DLL文件,在我的c#代码中在visual studio中创建。 I need to run my code from powershell, therefore I use the Add-Type method to load my assembly.
我需要从powershell运行我的代码,因此我使用Add-Type方法加载我的程序集。
My Powershell code: Add-Type -Path C:\\MyDirectoryWithDllFiles\\MyAssembly.dll [MyAssembly.MyClass]::MyStaticMethod()
我的Powershell代码:
Add-Type -Path C:\\MyDirectoryWithDllFiles\\MyAssembly.dll [MyAssembly.MyClass]::MyStaticMethod()
When I put return "Hello World" in MyStaticMethod, everything works fine. 当我在MyStaticMethod中放回“Hello World”时,一切正常。
Of course, my program needs some functionality and a config file was required. 当然,我的程序需要一些功能,并且需要配置文件。 I updated my powershell code to this:
我将我的powershell代码更新为:
[appdomain]::CurrentDomain.SetData("APP_CONFIG_FILE", "C:\MyDirectoryWithDllFiles\MyAssembly.dll.config")
Add-Type -AssemblyName System.Configuration
Add-Type -Path C:\MyDirectoryWithDllFiles\MyAssembly.dll
[MyAssembly.MyClass]::MyStaticMethod()
With the following config file: (MyAssembly.dll.config) 使用以下配置文件:(MyAssembly.dll.config)
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="myCustomSection" type="MyAssembly.MyCustomConfigurationSetting, MyAssembly" />
</configSections>
<!--this is the custom config section for myCustomSection-->
<myCustomSection>
<!-- some items -->
</myCustomSection>
</configuration>
In MyStaticMethod I get the items from my config file, and it works fine when I run the code from visual studio. 在MyStaticMethod中,我从配置文件中获取项目,当我从visual studio运行代码时,它工作正常。 When I run the powershell code, as described above, I get the following error:
当我运行powershell代码时,如上所述,我收到以下错误:
PS C:\\MyDirectoryWithDllFiles> [MyAssembly.MyClass]::MyStaticMethod() Errors: [System.Configuration.ConfigurationErrorsException: An error occurred creating the configuration section handler for myCustomSection: Could not load file or assembly 'MyAssembly' or one of its dependencies.
PS C:\\ MyDirectoryWithDllFiles> [MyAssembly.MyClass] :: MyStaticMethod()错误:[System.Configuration.ConfigurationErrorsException:为myCustomSection创建配置节处理程序时出错: 无法加载文件或程序集'MyAssembly'或其依赖项之一。 The system cannot find the file specified.
该系统找不到指定的文件。 (C:\\Users\\MyUserName\\AppData\\Local\\Temp\\tmp2EB6.tmp line 4) ---> System.IO.FileNotFoundException: Could not load file or assembly 'MyAssembly' or one of its dependencies.
(C:\\ Users \\ MyUserName \\ AppData \\ Local \\ Temp \\ tmp2EB6.tmp第4行)---> System.IO.FileNotFoundException:无法加载文件或程序集“MyAssembly”或其依赖项之一。 The system cannot find the file specified.
该系统找不到指定的文件。 at System.Configuration.TypeUtil.GetTypeWithReflectionPermission(IInternalConfigHost host, String typeString, Boolean throwOnError)
at System.Configuration.TypeUtil.GetTypeWithReflectionPermission(IInternalConfigHost host,String typeString,Boolean throwOnError)
It tries to find 'MyAssembly', as how I have defined it in the the in the config file. 它试图找到'MyAssembly',就像我在配置文件中定义它一样。 MyAssembly.dll is a dll I have created.
MyAssembly.dll是我创建的一个DLL。 But even though I have this line in my powershell it won't work: Add-Type -Path C:\\MyDirectoryWithDllFiles\\MyAssembly.dll
但即使我在我的powershell中有这一行它也行不通:Add-Type -Path C:\\ MyDirectoryWithDllFiles \\ MyAssembly.dll
Any suggestions how I can get this working? 有什么建议我怎么能这个工作?
Solution: 解:
Thanks to JayKul's answer at https://stackoverflow.com/a/23569983/1408786 感谢JayKul在https://stackoverflow.com/a/23569983/1408786上的回答
I added this to the config section, and now it works fine: Version=1.0.0.0, Culture=neutral, PublicKeyToken=null 我将此添加到配置部分,现在它工作正常: 版本= 1.0.0.0,Culture = neutral,PublicKeyToken = null
<section name="myCustomSection" type="MyAssembly.MyCustomConfigurationSetting, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.