本文介绍了我如何从ini文件中获取旧价值!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名新的C#开发人员,我使用C#类从ini文件中读取设置,但昨天我的应用从ini文件中读取了旧值!这怎么可能,有现金还是c#使用了什么?

I am a new C# developer and I use a C# class to read settings from ini file but yesterday my app read an old value from the ini file !! how is this possible, is there a cash or something c# use ?

这是我的代码:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Reflection; using System.Runtime.InteropServices; namespace admin { class inifile { string Path; string EXE = Assembly.GetExecutingAssembly().GetName().Name; [DllImport("kernel32", CharSet = CharSet.Unicode)] static extern long WritePrivateProfileString(string Section, string Key, string Value, string FilePath); [DllImport("kernel32", CharSet = CharSet.Unicode)] static extern int GetPrivateProfileString(string Section, string Key, string Default, StringBuilder RetVal, int Size, string FilePath); public inifile(string IniPath = null) { Path = new FileInfo(IniPath ?? EXE + ".ini").FullName.ToString(); } public string Read(string Key, string Section = null) { var RetVal = new StringBuilder(255); GetPrivateProfileString(Section ?? EXE, Key, "", RetVal, 255, Path); return RetVal.ToString(); } public void Write(string Key, string Value, string Section = null) { WritePrivateProfileString(Section ?? EXE, Key, Value, Path); } public void DeleteKey(string Key, string Section = null) { Write(Key, null, Section ?? EXE); } public void DeleteSection(string Section = null) { Write(null, null, Section ?? EXE); } public bool KeyExists(string Key, string Section = null) { return Read(Key, Section).Length > 0; } } }

,我这样使用它:

var MyIni = new inifile("admin.ini");
myvar = MyIni.Read("item","login").Trim();

var MyIni = new inifile("admin.ini");
myvar = MyIni.Read("item", "login").Trim();

任何建议?

推荐答案

.ini文件的整个概念已过时.显然,从NT开始,它会忽略您,并将内容放入注册表中.

This whole concept of the .ini file is outmoded.  Apparently from NT onward it ignores you and puts stuff in the registry anyway.

除非您使用的是Windows95,否则它将无法正常工作.

Unless you are using Windows95 it doesn't look like this will work as expected.

在此处查看如何使用应用设置"-您会发现它变得更加容易. https://msdn.microsoft.com/zh-CN/library/bb397750(v = vs.110).aspx

Look here for how to use App Settings - you'll find it a lot easier. https://msdn.microsoft.com/en-us/library/bb397750(v=vs.110).aspx


这篇关于我如何从ini文件中获取旧价值!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 19:24