经常能在.Net 项目中看到App.Config/Web.Config , 一直没有了解过.Net 自带的对配置文件的读写操作,常规的操作类在 System.Configuration.dll 中 ,比较重要的类为ConfigurationManager
底部有全部代码+单元测试
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="MyGroup" type="AppConfig.MyGroup,Appconfig">
<section name="MySection" type="AppConfig.MySection,Appconfig" />
</sectionGroup>
<section name="MySection" type="AppConfig.MySection,Appconfig" />
</configSections>
<appSettings>
<add key="A" value="a"/>
<add key="B" value="b"/>
<add key="C" value="c"/>
</appSettings>
<connectionStrings>
<add connectionString="123" name="1"/>
<add connectionString="456" name="2"/>
</connectionStrings>
<MySection Code="asdas">
<Member Id="dasd"/>
<Members>
<add Id="1"/>
<add Id="2"/>
<add Id="3"/>
</Members>
</MySection>
<MyGroup>
<MySection Code="asdas1">
<Member Id="dasd1"/>
<Members>
<add Id="12"/>
<add Id="22"/>
</Members>
</MySection>
</MyGroup>
</configuration>
指定AppSetting 的 读取
public string GetAppSettingValue(string appSettingName)
{
return ConfigurationManager.AppSettings.AllKeys.FirstOrDefault(item => item.Equals(appSettingName)) != null ? ConfigurationManager.AppSettings[appSettingName] : null;
}
指定ConnecString的获取
public string GetConnectString(string name)
{
return ConfigurationManager.ConnectionStrings[name]?.ConnectionString;
}
表示配置文件中的节。
// 自定义一个Section
internal class MySection : ConfigurationSection
{
[ConfigurationProperty(nameof(Code))]
public string Code
{
get => base[nameof(Code)].ToString();
}
}
需要在属性上使用ConfigurationPropertyAttribute
//调用方法
public string GetMySectionCode()
{
return (ConfigurationManager.GetSection("MySection") as MySection)?.Code;
}
表示Section的成员
//根据Config 配置属性
public class MyElement : ConfigurationElement
{
[ConfigurationProperty(nameof(Id))]
public string Id
{
get => this[nameof(Id)].ToString();
}
}
//同时在MySection 中进行扩展
internal class MySection : ConfigurationSection
{
...
[ConfigurationProperty(nameof(Member))]
public MyElement Member
{
get => base[nameof(Member)] as MyElement;
}
...
}
//调用方法
public string GetMySectionMember()
{
return (ConfigurationManager.GetSection("MySection") as MySection)?.Member?.Id;
}
表示Element的集合
//定义一个MyElement 的集合
[ConfigurationCollection(typeof(MyElement))]
internal class MyElementCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new MyElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return (element as MyElement).Id;
}
}
//在Section 中进行填充
internal class MySection : ConfigurationSection
{
[ConfigurationProperty(nameof(Members)), ConfigurationCollection(typeof(MyElement))]
public MyElementCollection Members
{
get => base[nameof(Members)] as MyElementCollection;
}
}
//调用
public int GetMySectionMembers()
{
return (int)(ConfigurationManager.GetSection("MySection") as MySection)?.Members?.Count;
}
在Section 外面在套一个类
public class MyGroup : ConfigurationSectionGroup
{
[ConfigurationProperty(nameof(MySection))]
public MySection MySection { get => this.Sections[nameof(MySection)] as MySection; }
}
// 两种调用方式都可以
public int GetMySectionMembersInGroup()
{
return (int)(ConfigurationManager.GetSection("MyGroup/MySection") as MySection)?.Members?.Count;
}
public int GetMySectionMembersInGroupByOpenConfig()
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var group = (MyGroup)config.GetSectionGroup("MyGroup");
return group.MySection.Members.Count;
}
ExeConfigurationFileMap exeConfigurationFileMap = new ExeConfigurationFileMap() { ExeConfigFilename = @".\App1.config" };
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(exeConfigurationFileMap,ConfigurationUserLevel.None);
这边加载方式与上面的不同 后续一致
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信
我一直致力于让我们的Rails2.3.8应用程序在JRuby下正确运行。一切正常,直到我启用config.threadsafe!以实现JRuby提供的并发性。这导致lib/中的模块和类不再自动加载。使用config.threadsafe!启用:$rubyscript/runner-eproduction'pSim::Sim200Provisioner'/Users/amchale/.rvm/gems/jruby-1.5.1@web-services/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:105:in`co
无论您是想搭建桌面端、WEB端或者移动端APP应用,HOOPSPlatform组件都可以为您提供弹性的3D集成架构,同时,由工业领域3D技术专家组成的HOOPS技术团队也能为您提供技术支持服务。如果您的客户期望有一种在多个平台(桌面/WEB/APP,而且某些客户端是“瘦”客户端)快速、方便地将数据接入到3D应用系统的解决方案,并且当访问数据时,在各个平台上的性能和用户体验保持一致,HOOPSPlatform将帮助您完成。利用HOOPSPlatform,您可以开发在任何环境下的3D基础应用架构。HOOPSPlatform可以帮您打造3D创新型产品,HOOPSSDK包含的技术有:快速且准确的CAD
我想解析一个已经存在的.mid文件,改变它的乐器,例如从“acousticgrandpiano”到“violin”,然后将它保存回去或作为另一个.mid文件。根据我在文档中看到的内容,该乐器通过program_change或patch_change指令进行了更改,但我找不到任何在已经存在的MIDI文件中执行此操作的库.他们似乎都只支持从头开始创建的MIDI文件。 最佳答案 MIDIpackage会为您完成此操作,但具体方法取决于midi文件的原始内容。一个MIDI文件由一个或多个音轨组成,每个音轨是十六个channel中任何一个上的
文章目录1.开发板选择*用到的资源2.串口通信(个人理解)3.代码分析(注释比较详细)1.主函数2.串口1配置3.串口2配置以及中断函数4.注意问题5.源码链接1.开发板选择我用的是STM32F103RCT6的板子,不过代码大概在F103系列的板子上都可以运行,我试过在野火103的霸道板上也可以,主要看一下串口对应的引脚一不一样就行了,不一样的就更改一下。*用到的资源keil5软件这里用到了两个串口资源,采集数据一个,串口通信一个,板子对应引脚如下:串口1,TX:PA9,RX:PA10串口2,TX:PA2,RX:PA32.串口通信(个人理解)我就从串口采集传感器数据这个过程说一下我自己的理解,
s=Socket.new(Socket::AF_INET,Socket::SOCK_STREAM,0)s.connect(Socket.pack_sockaddr_in('port','hostname'))ssl=OpenSSL::SSL::SSLSocket.new(s,sslcert)ssl.connect从这里开始,如果ssl连接和底层套接字仍然是ESTABLISHED,或者它是否在默认值7200之后进入CLOSE_WAIT,我想检查一个线程几秒钟甚至更糟的是在实际上不需要.write()或.read()的情况下关闭。是用select()、IO.select()还是其他方法完成
在Ruby1.9中,我如何从ARGF中读取CSV?我尝试了以下方法,但没有打印任何内容:require'csv'CSV(ARGF).readdo|row|prowendhttp://www.ruby-doc.org/core-1.9.3/ARGF.htmlhttp://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV.html 最佳答案 如果你想偷懒你可以试试:CSV.new(ARGF.file).eachdo|row|...end来源:http://www.ruby-doc.org/std
我正在编写一个ruby程序,它应该执行另一个程序,通过stdin向它传递值,从它的stdout读取响应,然后打印响应。这是我目前所拥有的。#!/usr/bin/envrubyrequire'open3'stdin,stdout,stderr=Open3.popen3('./MyProgram')stdin.puts"helloworld!"output=stdout.readerrors=stderr.readstdin.closestdout.closestderr.closeputs"Output:"puts"-------"putsoutputputs"\nErrors:"p
我们如何从ruby脚本返回值?#!/usr/bin/envrubya="test"a我们如何在Ubuntu终端或java或c中访问'a'的值? 最佳答案 在ruby/python脚本中打印你的变量,然后可以通过示例从shell脚本中读取它:#!/bin/bashruby_var=$(rubymyrubyscript.rb)python_var=$(pythonmypythonscript.py)echo"$ruby_var"echo"$python_var"注意你的ruby/python脚本只打印这个变量(有更多复杂的方