从C#应用程序中的配置文件中检索值不需要';行不通

从C#应用程序中的配置文件中检索值不需要';行不通,c#,config,C#,Config,我遇到了一个问题,当我尝试使用配置文件时, 我在这里和其他地方读过一些帖子 但我不能解决工作中的问题 在这里的问题中,我添加了配置 在文件中声明 string ab123 = ConfigurationManager.AppSettings["ab123"]; 但在侧面,我表明错误是错误的 “未处理win32异常-系统找不到指定的文件” 当我运行此代码时,ab123值始终为空! 我相信这条路是正常的 如何修复它?听起来指定的文件不存在:D:\ab123\Source\a

我遇到了一个问题,当我尝试使用配置文件时, 我在这里和其他地方读过一些帖子 但我不能解决工作中的问题

在这里的问题中,我添加了配置


在文件中声明

string ab123 = ConfigurationManager.AppSettings["ab123"];
但在侧面,我表明错误是错误的 “未处理win32异常-系统找不到指定的文件”

当我运行此代码时,
ab123
值始终为空! 我相信这条路是正常的


如何修复它?

听起来指定的文件不存在:
D:\ab123\Source\ab123.c

另外,您是否正在尝试存储键值对?比如说:

  <setting name="ab123" serializeAs="String">
    <value>D:\ab123\Source\ab123.c</value>
  </setting>

D:\ab123\Source\ab123.c

如果您指的是应用程序的
App.Config
文件,那么我认为您在设置中另外指定了
add
元素:

设置应如下所示:

 <setting name="ab123" serializeAs="String">            
       <value>D:\\ab123\\Source\\ab123.c</value>
 </setting>

看起来你混合了两种型号

  • 通过声明派生自的类来定义,尽管通常不会手动执行此操作。相反,让VS通过转到项目的“属性”>“设置”选项卡为您创建类。这将在您的配置文件中创建一个特殊部分,该部分看起来与您发布的XML相似

  • 在特殊的
    appSettings
    配置部分中定义。正如您所做的那样,只能通过
    ConfigurationManager.AppSettings
    属性访问这些类型的设置。请参见下面的XML示例
  • 
    
    ,即使指定的路径为空,也会引发该错误。当在配置文件中找不到指定的设置时,
    ConfigurationManager.AppSettings
    返回空引用。(为了清楚起见,在您的案例中找不到它,因为您没有使用如上所示的
    appSettings
    config部分。)

    我建议另一个答案。我假设这是您的配置文件的摘录(而不是全部内容)

    <setting name="ab123" serializeAs="String">
        <add key="ab123" value="D:\ab123\Source\ab123.c"/>
    </setting>
    
    您已经声明文件D:\ab123\Source\ab123.c存在。因此我假设这是真的

    如果所有这些都是真的,我可以看到她的一个问题是,您没有将可执行文件或批处理/cmd文件指定给
    System.Diagnostics.Process.Start
    。您的代码相当于

    ConfigurationManager.AppSettings("D:\\ab123\\Source\\ab123.c");
    
    这是可以接受的,但Windows将看到ab123.c不是可执行程序。然后,它会在Windows注册表中查找
    .c
    文件关联。文件关联告诉Windows如何打开此类文件。如果存在文件关联,它将启动程序,指定D:\ab123\Source\ab123.c作为其参数(例如,这样的程序将是VisualStuido)

    如果存在文件关联,那么如果上述所有假设都正确,那么您所做的调用应该会起作用。但是,如果没有将
    .c
    与程序关联,则Windows将不知道如何打开它,并且您将得到类似于此的错误:

    System.dll中发生类型为“System.ComponentModel.Win32Exception”的未处理异常 其他信息:系统找不到指定的文件

    System.Diagnostics.Process.Start(ConfigurationManager.AppSettings["ab123"]);
    
    检查扩展是否有默认程序的一种方法是,只需打开cmd.exe终端外壳并发出以下命令:

    D:\ab123\Source\ab123.c
    

    如果启动了一个程序并加载了您的文件,那么关联就存在,我的答案将不适用。我建议这是一个答案,以防有人在此处搜索并登录。这可能对其他人有帮助,但可能不是OP的实际问题。

    从您的xml配置文件中可以看出您确实在尝试使用它用户设置而不是应用程序设置,并且您已经混合了一些想法。我认为更正确的配置版本可能是:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <configSections>
            <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
                <section name="CA.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
            </sectionGroup>
        </configSections>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
        </startup>
        <userSettings>
          <CA.Properties.Settings>
            <setting name="ab123" serializeAs="String">
              <value>D:\ab123\Source\ab123.c</value>
            </setting>
            </CA.Properties.Settings>
        </userSettings>
    </configuration>
    
    如果您使用Settings.Settings创建和删除设置,则代码可以简化为此,因为Visual Studio将为您的设置对象创建绑定,这些绑定可在设计时和运行时访问。有关通过Visual Studio IDE使用Settings.Settings的信息,请参阅此。如果由于某种原因,下面的代码不可用't work您可以使用上面的代码:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Configuration;
    using System.Diagnostics;
    using System.IO;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string sFileName = Properties.Settings.Default.ab123;
    
                // Check if ab123 has a value and the file exists
                if (!string.IsNullOrEmpty(sFileName) && System.IO.File.Exists(sFileName))
                {
                    using (StreamReader sr = new StreamReader(sFileName))
                    {
                        string line;
                        // Read and display lines from the file until the end of  
                        // the file is reached. 
                        while ((line = sr.ReadLine()) != null)
                        {
                            System.Diagnostics.Debug.WriteLine(line);
                        }
                    }
                }
            }
        }
    }
    

    关注问题。首先获取配置值,然后尝试启动一个进程。您没有获取值,这是一个合理的问题,但进程启动失败只是一个副产品。对于您的问题本身,请遵循此处给出的一些答案:@Jonesy该问题是关于标准配置键的,而不是自定义配置部分…@OferZelig它的意思是无法获取值?在您的xml文件中,我不明白
    在配置管理器的上下文中做了什么。如果它被删除会发生什么?@kyoko如果它与之前的错误完全相同,那么一定有其他问题。请尝试在a中重现该问题,包括所有C代码和xml配置。I C见鬼,该文件位于此路径I fix xml但不是此问题请尝试在以下行后设置断点:
    string ab123=ConfigurationManager.AppSettings[“ab123”];
    ,然后查看ab123的值(确保它是有效路径)。我尝试了,但出现问题“win32异常未处理-系统找不到指定的文件”您是否将其放在了.exe项目的app.config文件中?为.dll项目生成的配置文件根本不相关。是的,因为您使用的是
    appSettings
    ,正如我在回答中所述。请注意,它与您在回答的注释中向我描述的
    setting
    不一样,它是这不是OP发布的内容。是的,我也有一点困惑。我认为在这一点上OP需要遵循我的建议,发布一个简短但完整的示例。根据另一个答案上的最新评论,他似乎试图使用
    Start
    来“读取”一个文件,因此有bigg
    string ab123 = ConfigurationManager.AppSettings["ab123"];
    
    ConfigurationManager.AppSettings("D:\\ab123\\Source\\ab123.c");
    
    D:\ab123\Source\ab123.c
    
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <configSections>
            <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
                <section name="CA.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
            </sectionGroup>
        </configSections>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
        </startup>
        <userSettings>
          <CA.Properties.Settings>
            <setting name="ab123" serializeAs="String">
              <value>D:\ab123\Source\ab123.c</value>
            </setting>
            </CA.Properties.Settings>
        </userSettings>
    </configuration>
    
    <setting name="ab123" serializeAs="String">
        <value>D:\ab123\Source\ab123.c</value>
    </setting>
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Configuration;
    using System.Diagnostics;
    using System.IO;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                // Retrieve the userSettings gorup
                ConfigurationSectionGroup group = config.SectionGroups[@"userSettings"];
                if (group == null) return;
    
                // Get the program settings
                ClientSettingsSection clientSection = group.Sections["CA.Properties.Settings"] as ClientSettingsSection;
                if (clientSection == null) return;
    
                // This retrieves the value associated with the key
                string sFileName = clientSection.Settings.Get("ab123").Value.ValueXml.InnerText;
    
                // Check if ab123 has a value and the file exists
                if (!string.IsNullOrEmpty(sFileName) && System.IO.File.Exists(sFileName))
                {
                    using (StreamReader sr = new StreamReader(sFileName))
                    {
                        string line;
                        // Read and display lines from the file until the end of  
                        // the file is reached. 
                        while ((line = sr.ReadLine()) != null)
                        {
                            System.Diagnostics.Debug.WriteLine(line);
                        }
                    }
                }
            }
        }
    }
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Configuration;
    using System.Diagnostics;
    using System.IO;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string sFileName = Properties.Settings.Default.ab123;
    
                // Check if ab123 has a value and the file exists
                if (!string.IsNullOrEmpty(sFileName) && System.IO.File.Exists(sFileName))
                {
                    using (StreamReader sr = new StreamReader(sFileName))
                    {
                        string line;
                        // Read and display lines from the file until the end of  
                        // the file is reached. 
                        while ((line = sr.ReadLine()) != null)
                        {
                            System.Diagnostics.Debug.WriteLine(line);
                        }
                    }
                }
            }
        }
    }