将路径附加到字符串
本文关键字:字符串 路径 | 更新日期: 2023-09-27 17:52:48
我需要帮助将字符串添加到path。这里的问题是,我已经声明的路径不能调用,相反,它只是给正常的字符串值。这是我的代码。
public static string inputhistory1 = System.IO.Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]) + "''Log''" + Process.GetCurrentProcess().ProcessName + DateTime.Now.ToString("yyyyMMdd")+".chf";
public static string inputhistory2 = System.IO.Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]) + "''Log''FileExtact" + DateTime.Now.AddMonths(-1).ToString("yyyyMM") + ".chf";
public static string inputhistory3 = System.IO.Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]) + "''Log''FileExtact" + DateTime.Now.AddMonths(-2).ToString("yyyyMM") + ".chf";
public static string inputhistory4 = System.IO.Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]) + "''Log''FileExtact" + DateTime.Now.AddMonths(-3).ToString("yyyyMM") + ".chf";
public static bool checkfile(string filename)
{
bool same = false;
for (i = 1; i <= 4; i++)
{
string filechf = "inputhistory" + i;
filechf = filechf;
try
{
foreach (string line in System.IO.File.ReadAllLines(filechf))
{
if (line.Contains(filename))
{
same = true;
break;
}
else
{
same = false;
}
}
}
catch (Exception)
{
// Ignore if file does not exist.
}
if (same == true)
{
break;
}
}
}
只是为了展示LINQ的表现力,以及利用可用工具的强大功能:
List<string> inputHistories = new List<string>
{
inputhistory1, inputhistory2, inputhistory3, inputhistory4
};
public static bool checkfile(string filename)
{
return inputHistories.Any(filename =>
File.ReadLines(filename).Any(line => line.Contains(filename)));
}
这是因为您使用字符串"inputhistory" + i
分配变量filechf
。使用数组或列表来存储输入历史值。
public static string inputhistory1 = System.IO.Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]) + "''Log''" + Process.GetCurrentProcess().ProcessName + DateTime.Now.ToString("yyyyMMdd")+".chf";
public static string inputhistory2 = System.IO.Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]) + "''Log''FileExtact" + DateTime.Now.AddMonths(-1).ToString("yyyyMM") + ".chf";
public static string inputhistory3 = System.IO.Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]) + "''Log''FileExtact" + DateTime.Now.AddMonths(-2).ToString("yyyyMM") + ".chf";
public static string inputhistory4 = System.IO.Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]) + "''Log''FileExtact" + DateTime.Now.AddMonths(-3).ToString("yyyyMM") + ".chf";
List<string> inputHistories = new List<string>();
inputHistories.Add(inputhistory1);
inputHistories.Add(inputhistory2);
inputHistories.Add(inputhistory3);
inputHistories.Add(inputhistory4);
那么你可以通过索引访问它的值:
public static bool checkfile(string filename)
{
bool same = false;
//try
//{
for (i = 0; i < inputHistories.Count; i++)
{
string filechf = inputHistories[i];
try
{
foreach (string line in System.IO.File.ReadAllLines(filechf))
{
if (line.Contains(filename))
{
same = true;
break;
}
else
{
same = false;
}
}
}
catch (Exception)
{
//ignore if file does not exist
}
if (same == true)
{
break;
}
}
有多种解决方案可以满足您的需求
-
您可以将变量存储在字典中:
var dictionary = new Dictionary<string,string>(); dictionary.Add("inputhistory1", inputhistory1); dictionary.Add("inputhistory2", inputhistory2); dictionary.Add("inputhistory3", inputhistory3); dictionary.Add("inputhistory4", inputhistory4); //use as below Console.WriteLine(dictionary["inputhistory1"]);
-
或者您可以使用反射,有关更多信息MSDN:
public class TestClass { public static string inputhistory1 = "value1"; public static string inputhistory2 = "value2"; public static string inputhistory3 = "value3"; public static string inputhistory4 = "value4"; } var obj = new TestClass(); var field = typeof (TestClass).GetField("inputhistory1"); //use as below Console.WriteLine(field.GetValue(obj));
-
即使你可以使用switch/case返回你的变量值