Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.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# 添加类型a类型,它无法读取ConfigurationManager.AppSettings[";…";]?_C#_Powershell - Fatal编程技术网

C# 添加类型a类型,它无法读取ConfigurationManager.AppSettings[";…";]?

C# 添加类型a类型,它无法读取ConfigurationManager.AppSettings[";…";]?,c#,powershell,C#,Powershell,有一个库包含以下方法 public class Class1 { public void X() { //.... var user = ConfigurationManager.AppSettings["UserName"]; Console.WriteLine(user); // Nothing printed. 它由Powershell调用 $dllFolder = "C:\...\bin\Debug" Add-Typ

有一个库包含以下方法

public class Class1 
{
    public void X() 
    {
        //....
        var user = ConfigurationManager.AppSettings["UserName"];
        Console.WriteLine(user); // Nothing printed.
它由Powershell调用

$dllFolder = "C:\...\bin\Debug"

Add-Type -Path "$dllFolder\Class1.dll"
$test = new-object Class1
$test.X()
如何在dll中分配
ConfigurationManager.AppSettings[“用户名”]

编辑:

我向库项目添加了
app.config
,文件
Class1.dll.config
包含以下几行

<appSettings>
  <add key="UserName" value="...." />
</appSettings>


但是,在powershell中运行时,它仍然不会打印值

在app.config或web.config中,无论您的项目已添加以下内容

<configuration>  

    ...

    <appSettings>
      <add key="UserName" value="whatever you want the value to be" />
    </appSettings>

    ....

</configuration>

...
....
编辑:


如果您有权访问代码,请在根目录中创建app.config并重新生成。如果没有,请在build文件夹中查找config.ini并更新UserName属性。它应该已经在那里了。

以下几行解决了这个问题

[appdomain]::CurrentDomain.SetData("APP_CONFIG_FILE", "$dllFolder\Class1.dll.config")

这是什么样的申请?它有app.config或web.config吗?我在文件夹“C:\…\bin\Debug\”中创建了一个
app.config
,但不工作。您有权访问此应用程序吗?您是在开发它,还是在使用一个无法更改代码的.dll?我现在有了源代码。我尽量避免更改它,因为它不是我的代码(其他开发人员可能会更改它)。如果必须,我可能需要更改它。如果您有权访问该代码,请在根目录中创建app.config并重新生成。如果没有,请在build文件夹中查找config.ini并更新UserName属性。它应该已经在那里了。我添加了app.config,文件
Class1.dll.config
是用
创建的';这是一种有点老套的方式来做我想解释的事情。但如果它能起作用。。。