C# 从一个公共部分获取变量到另一个公共部分
这可能是一个非常简单的noob问题。基本上,我有一个txt文件,其中有许多变量控制我编写的服务。目前,在每个需要变量的空间中,我让它打开文件,读取文件,选择行,然后使用变量 我的问题是,是否有一种全局分配变量的方法。这样我所有的空隙都可以使用它们。这样,我只需要在读取所有内容后打开文件,为所有内容分配一个变量。而不是我现在这样做,我打开文件多次在大多数情况下寻找相同的变量 我编辑了我的问题并添加了一些代码,试图更好地解释。 下面我有两个空隙。它们都在打开同一个文件,但在文件中查找不同的行。我想知道是否有可能创建一个“全局”变量列表来读取整个文件,然后我就可以调用我需要的变量,而不是每次需要信息时都打开文件C# 从一个公共部分获取变量到另一个公共部分,c#,C#,这可能是一个非常简单的noob问题。基本上,我有一个txt文件,其中有许多变量控制我编写的服务。目前,在每个需要变量的空间中,我让它打开文件,读取文件,选择行,然后使用变量 我的问题是,是否有一种全局分配变量的方法。这样我所有的空隙都可以使用它们。这样,我只需要在读取所有内容后打开文件,为所有内容分配一个变量。而不是我现在这样做,我打开文件多次在大多数情况下寻找相同的变量 我编辑了我的问题并添加了一些代码,试图更好地解释。 下面我有两个空隙。它们都在打开同一个文件,但在文件中查找不同的行。我想知
public void day_timer()
{
string temptimer = "";
string timeloop = "";
using (var streamReader = System.IO.File.OpenText(@"C:\somefile"))
{
var lines = streamReader.ReadToEnd().Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines)
{
if (line.Contains("Day_Time:"))
{
temptimer = line;
continue;
}
}
}
timeloop = temptimer.Remove(0, 9);
int inter = Convert.ToInt32(timeloop);
System.Timers.Timer timer1 = new System.Timers.Timer();
InitializeComponent();
timer1.Elapsed += new ElapsedEventHandler(timer1_Elapsed);
timer1.Interval = inter * 1000 * 60;
timer1.Enabled = true;
timer1.Start();
error_handling("backup started " + DateTime.Now + ". Incremental Backup set to every " + timeloop + " Minutes", "Incbackuplog.txt");
}
//Incrememtal Backup Timer
public void inc_timer()
{
string temptimer = "";
string timeloop = "";
using (var streamReader = System.IO.File.OpenText(@"C:\somefile"))
{
var lines = streamReader.ReadToEnd().Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines)
{
if (line.Contains("Inc_interval:"))
{
temptimer = line;
continue;
}
}
}
timeloop = temptimer.Remove(0, 13);
int inter = Convert.ToInt32(timeloop);
System.Timers.Timer timer1 = new System.Timers.Timer();
InitializeComponent();
timer1.Elapsed += new ElapsedEventHandler(timer1_Elapsed);
timer1.Interval = inter * 1000 * 60;
timer1.Enabled = true;
timer1.Start();
error_handling(" Backup Started "+DateTime.Now+". Incremental Backup set to every "+timeloop+" Minutes", "Incbackuplog.txt");
}
IMHO我建议对文件中的设置使用静态类: 这样,只有在第一次访问类时才读取文件 希望这样的东西适合你: 更新: 填写读取文件逻辑并添加属性调用
public static class Configuration
{
// public set so you can change the configfile location if necessary
public static string ConfigurationFile { get; set; }
// variables from configuration file, private set so they cannot be changed except by changing the configuration and reloading
public static string Inc_interval { get; private set; }
public static string Day_Time { get; private set; }
/// <summary>
/// Static constructor - will be called the first time this class is accessed
/// </summary>
static Configuration()
{
ConfigurationFile = @"C:\somefile";
ReadConfigFile();
}
/// <summary>
/// Calling this method will reload the configuration file
/// </summary>
public static void Reload()
{
ReadConfigFile();
}
/// <summary>
/// Logic for reading the configuration file
/// </summary>
private static void ReadConfigFile()
{
// ToDo: Read file here and fill properties
using (StreamReader rd = new StreamReader(ConfigurationFile))
{
while (!rd.EndOfStream)
{
string line = rd.ReadLine();
if (line.StartsWith("Day_Time:"))
Day_Time = line.Remove(0, 9);
else if (line.StartsWith("Inc_interval:"))
interval = line.Remove(0, 13);
}
}
}
}
有很多方法可以做到这一点,如果没有一些代码或更多信息,很难推荐路径。是的。你有什么特别的问题?发布您当前的代码可能会有所帮助,因为它可能会突出显示阻止您的原因。但是你似乎从你的描述中理解了你需要做什么,那么问题是什么呢?如果你在寻找建议,那么如果我要实现这样一个功能,我将创建一个类,其属性与文本文件中的变量匹配,并具有一些标志,如
bool isFileRead
。然后,当我想使用一些变量时,我首先会检查标志-如果isFileRead
等于false,我将读取文本文件并赋值,否则它已经被读取并使用类属性。有趣的是,这有点整洁。我一直试图通过虚拟学院了解更多关于班级的知识,但你上面的例子很好。那么我可以从任何地方调用变量,对吗?是的,因为属性是静态的,所以不需要该类的对象。第一次实际使用此类时,静态构造函数将被调用并读取文件。此外,该类被声明为public,因此您可以从项目的所有其他类/方法调用它。这太好了,谢谢您!!这肯定会让事情变得容易一些。
timeloop = Configuration.Day_Time;