C# C将变量从文本文件读入静态字符串
我看过几篇文章给出了如何读取文本文件的示例,以及如何使字符串“public”为静态或常量的示例,但我还没有能够以一种对我有意义的方式将两者结合在“function”中 我有一个名为“MyConfig.txt”的文本文件。 其中,我有两行C# C将变量从文本文件读入静态字符串,c#,global-variables,text-files,C#,Global Variables,Text Files,我看过几篇文章给出了如何读取文本文件的示例,以及如何使字符串“public”为静态或常量的示例,但我还没有能够以一种对我有意义的方式将两者结合在“function”中 我有一个名为“MyConfig.txt”的文本文件。 其中,我有两行 MyPathOne=C:\TestOne MyPathTwo=C:\TestTwo 我希望能够在启动表单时读取该文件,使MyPathOne和MyPathTwo都可以从表单中的任何位置访问,使用如下方式: ReadConfig("MyConfig.txt")
MyPathOne=C:\TestOne
MyPathTwo=C:\TestTwo
我希望能够在启动表单时读取该文件,使MyPathOne和MyPathTwo都可以从表单中的任何位置访问,使用如下方式:
ReadConfig("MyConfig.txt");
我现在尝试这样做的方式是这样的,但不起作用:
public voice ReadConfig(string txtFile)
{
using (StreamReader sr = new StreamResder(txtFile))
{
string line;
while ((line = sr.ReadLine()) !=null)
{
var dict = File.ReadAllLines(txtFile)
.Select(l => l.Split(new[] { '=' }))
.ToDictionary( s => s[0].Trim(), s => s[1].Trim());
}
public const string MyPath1 = dic["MyPathOne"];
public const string MyPath2 = dic["MyPathTwo"];
}
}
txt文件可能永远不会超过5或6行,我也不会一直使用StreamReader或dictionary
只要我可以从任何地方按名称访问路径变量,并且不会添加400行代码之类的内容,那么我就可以做最好、最安全、最快、最简单的事情
我读过很多帖子,人们说数据应该存储在XML中,但我认为这一部分其实并不重要,因为读取文件和获取变量部分几乎是一样的。除此之外,我更希望能够使用一个普通的txt文件,最终用户无需理解XML即可编辑该文件。当然,这意味着要对空行进行大量检查,路径是否存在,等等…我可以做那个部分,只是想先让这个部分工作
我读过关于使用ReadAllLines到数组中的不同方法,有人说创建一个新的单独的“类”文件,我还不太了解。但是正在处理它。我主要想找到一种“稳定”的方法来实现这一点。
顺便说一下,项目正在使用.Net4和Linq
谢谢 您需要在函数外部定义变量,以便其他函数可以访问这些变量
public string MyPath1; // (Put these at the top of the class.)
public string MyPath2;
public voice ReadConfig(string txtFile)
{
var dict = File.ReadAllLines(txtFile)
.Select(l => l.Split(new[] { '=' }))
.ToDictionary( s => s[0].Trim(), s => s[1].Trim()); // read the entire file into a dictionary.
MyPath1 = dict["MyPathOne"];
MyPath2 = dict["MyPathTwo"];
}
您提供的代码甚至没有编译。相反,您可以尝试以下方法:
public string MyPath1;
public string MyPath2;
public void ReadConfig(string txtFile)
{
using (StreamReader sr = new StreamReader(txtFile))
{
// Declare the dictionary outside the loop:
var dict = new Dictionary<string, string>();
// (This loop reads every line until EOF or the first blank line.)
string line;
while (!string.IsNullOrEmpty((line = sr.ReadLine())))
{
// Split each line around '=':
var tmp = line.Split(new[] { '=' },
StringSplitOptions.RemoveEmptyEntries);
// Add the key-value pair to the dictionary:
dict[tmp[0]] = dict[tmp[1]];
}
// Assign the values that you need:
MyPath1 = dict["MyPathOne"];
MyPath2 = dict["MyPathTwo"];
}
}
考虑到:
不能将公共字段声明为方法
不能在运行时初始化常量字段。而是在编译时为它们提供一个常量值
明白了。谢谢
public static string Path1;
public static string Path2;
public static string Path3;
public void ReadConfig(string txtFile)
{
using (StreamReader sr = new StreamReader(txtFile))
{
var dict = new Dictionary<string, string>();
string line;
while (!string.IsNullOrEmpty((line = sr.ReadLine())))
{
dict = File.ReadAllLines(txtFile)
.Select(l => l.Split(new[] { '=' }))
.ToDictionary( s => s[0].Trim(), s => s[1].Trim());
}
Path1 = dict["PathOne"];
Path2 = dict["PathTwo"];
Path3 = Path1 + @"\Test";
}
}
这个问题类似于
我把答案放在那里。我不能把它贴在这里
不确定是否应将此问题标记为重复。标志关闭
重复的问题是否得到整合?每一个都可以在[通常是蹩脚的]问题或[意义不足和范围过大的]答案的措辞上有优点。合并版本可能具有最好的功能,但合并很少是微不足道的。是否可能有一个类和一个数组或列表来保存文本文件中的行。然后可以创建一个从任何地方都可以访问的单例对象。当您实例化singleton对象时,读取文件。-什么与您当前的代码不兼容?谢谢!如果我想添加MyPath3,并使其类似于MyPath3=MyPath1+@\Test并能够引用它,我会在最上面定义它,它会起作用吗,或者我会使用某种“组合”语句吗??