【问题标题】:Append path to string将路径附加到字符串
【发布时间】:2016-06-29 02:13:42
【问题描述】:

我需要帮助将字符串添加到路径。这里的问题是我声明的路径不能被调用,而是它只是给出正常的字符串值。这是我的代码。

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;
        }
    }
}

【问题讨论】:

标签: c# asp.net string for-loop


【解决方案1】:

只是为了展示 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)));
}

【讨论】:

  • 也许@jejakakhai Jk 想在使用字符串时从变量中检索值
  • @TommyLike:我无法读懂他的想法。我刚刚创建了代码来复制他发布的内容。我不得不假设,如果他想要不同的东西,他会要求的。
【解决方案2】:

那是因为你用字符串"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;
            }
        }

【讨论】:

  • 索引应该从 0 开始
【解决方案3】:

有多种解决方案可以满足您的要求

  1. 您可以将变量存储在字典中:

    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"]);
    
  2. 或者你可以使用反射,更多信息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));
    
  3. 即使你可以使用 switch/case 来返回你的变量值

【讨论】:

    猜你喜欢
    • 2019-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    • 2012-05-28
    相关资源
    最近更新 更多