如何在C#Winforms中的app.config中保存配置

有人能举例说明如何使用C#和WinForms在app.config中保存键/值吗?

在数据绑定到ComboBox中的CollectionViewSource时,如何保留CurrentItem的TwoWay绑定

假设我们有一个简单的VM类 public class PersonViewModel : Observable { private Person m_Person= new Person(“Mike”, “Smith”); private readonly ObservableCollection m_AvailablePersons = new ObservableCollection( new List { new Person(“Mike”, “Smith”), new Person(“Jake”, “Jackson”), }); public ObservableCollection AvailablePersons { get { return m_AvailablePersons; } } public Person CurrentPerson { get { return m_Person; } set { m_Person = value; NotifyPropertyChanged(“CurrentPerson”); […]

unit testing具有事件和委托的类

我是新手测试请帮助。 我有以下课程 public delegate void OnInvalidEntryMethod(ITnEntry entry, string message); public class EntryValidator { public event OnInvalidEntryMethod OnInvalidEntry; public bool IsValidEntry(ITnEntry entry, string ticker) { if (!IsFieldValid(entry, ticker.Trim().Length.ToString(), “0”)) return false; return true; } private bool IsFieldValid(ITnEntry entry, string actual, string invalidValue) { if (actual == invalidValue) { RaiseInvalidEntryEvent(entry); return false; } return true; } private […]

如何从diff应用程序中读取app.config中的用户设置?

我有一个带有App.config的WinForms .exe,它有一堆在运行时设置并保存的User Scoped设置。 我希望能够使用WinForms应用程序更改并保存设置,然后单击按钮以根据这些设置执行某些操作。 我还想从sep中读取同一.config文件中的用户设置。 控制台应用程序,所以我可以安排工作作为计划任务完成。 能够做到这一点的最佳方法是什么? 更新:我尝试了使用ConfigurationManager.OpenExeConfiguration的建议,如某些答案中所述。 Configuration config = ConfigurationManager.OpenExeConfiguration(“F:\\Dir\\App.exe”); 但是当我尝试检索这样的用户设置时。 string result = config.AppSettings.Settings[“DB”].ToString(); 我得到一个Null引用错误。 从exe中的代码,但以下正确返回数据库名称。 Properties.Settings.Default.DB 我哪里错了? 更新2: 因此,根据下面的一些答案,我现在可以使用以下内容从sep中检索我感兴趣的user.config文件部分的原始XML。 ConsoleApp。 System.Configuration.ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap(); fileMap.ExeConfigFilename = @”D:\PathHere\user.config”; Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap,ConfigurationUserLevel.None); System.Configuration.DefaultSection configSection = (System.Configuration.DefaultSection)config.GetSection(“userSettings”); string result = configSection.SectionInformation.GetRawXml(); Console.WriteLine(result); 但我仍然无法为我感兴趣的特定元素提取值。

需要一次一个地遍历SQL表行(表太大而无法使用adapter.Fill)

我很容易通过这样的小型SQL Server 2005表进行读取: string cmdText = “select * from myTable”; SqlDataAdapter adapter = new SqlDataAdapter(cmdText, connection); DataTable table = new DataTable(); adapter.Fill(table); 不幸的是,这种方法似乎将整个表加载到内存中,这根本不适用于我正在使用的巨大表。 我希望能够一次一行地遍历表,这样一次只需要一行存储在内存中。 有点像: foreach (DataRow row in rowIteratorObject) { // do something using the row // current row goes out of scope and is no longer in memory } 类似于您可以使用StreamReader一次处理一行文本文件的方式,而不是一次性读取所有文本文件。 有没有人知道用表行做这个的方法(或者,如果我咆哮错误的树,另一种解决方案)?

在Windows Phone 8.1上获取CivicAddress

我正试图从Windows Phone 8.1中的Geoposition获取CivicAddress 我尝试使用以下代码: // Get Current Location var geolocator = new Geolocator(); geolocator.DesiredAccuracyInMeters = 100; var position = await geolocator.GetGeopositionAsync(); // Get Country var country = position.CivicAddress.Country; 由于CivicAddress字段为null,因此抛出NullReferenceException。 我知道没有为Windows 8提供CivicAddress提供程序。我想检查是否是Windows Phone 8.1的情况。如果是这样,我如何获取/编写CivicAddress提供程序?

ConfigurationManager.AppSettings返回null

我正在使用ConfigurationManager.AppSettings [myKey]从我的Windows应用程序中的app.config文件中读取一个值,但返回的值始终为null,即使该键存在且它有值,Deas任何人都知道为什么? 谢谢

2个字符串之间的文本的正则表达式模式

我试图在以下模式中提取所有文本(显示为xxxx): Session[“xxxx”] 使用c# 这可能是Request.Querystring[“xxxx”]所以我试图动态构建表达式。 当我这样做,我得到各种关于未转义的字符或没有匹配的问题:( 一个例子可能是: string patternstart = “Session[“; string patternend = “]”; string regexexpr = @”\\” + patternstart + @”(.*?)\\” + patternend ; string sText = “Text to be searched containing Session[\”xxxx\”] the result would be xxxx”; MatchCollection matches = Regex.Matches(sText, @regexexpr); 我可以帮助解决这个问题(因为我似乎总是使用RegEx :))

如何将DataTable中的值转换为c#中的字符串数组

我有一个包含单行的DataTable。 我想将此DataTable值转换为字符串数组,以便我可以通过字符串数组索引访问该DataTable的列值例如,如果我的DataTable如下所示 | Name | Address | Age | ——————————- | jim | USA | 23 | 我想将Datatable中的值存储到我的字符串数组中,以便MyStringArray [1]为我提供值USA。 提前致谢

WS-Security中PasswordDigest的工作算法

我遇到了WS-Security的问题,并创建了一个正确的随机数和密码摘要。 我成功使用SoapUI将数据发送到Oracle系统。 所以我能够拦截SoapUI的调用(将代理更改为127.0.0.1端口8888以使用Fiddler失败,因为它是通过SSL) – 拦截非常重要,因为这些值只能使用一次。 然后我可以获取nonce,创建时间戳和密码摘要将它们放入我的代码中(我只有30秒的时间来执行此操作,因为值不会持续!)并且我获得了成功。 所以我知道它不是别的 – 只是密码摘要。 我使用的值如下: Nonce: UIYifr1SPoNlrmmKGSVOug== Created Timestamp: 2009-12-03T16:14:49Z Password: test8 Required Password Digest: yf2yatQzoaNaC8BflCMatVch/B8= 我知道创建摘要的算法是: Password_Digest = Base64 ( SHA-1 ( nonce + created + password ) ) 使用以下代码(来自Rick Strahl的post ) protected string GetSHA1String(string phrase) { SHA1CryptoServiceProvider sha1Hasher = new SHA1CryptoServiceProvider(); byte[] hashedDataBytes = sha1Hasher.ComputeHash(Encoding.UTF8.GetBytes(phrase)); return Convert.ToBase64String(hashedDataBytes); } […]