首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将路径追加到字符串

将路径追加到字符串
EN

Stack Overflow用户
提问于 2016-06-29 02:13:42
回答 3查看 134关注 0票数 0

我需要帮助把绳子伸向路径。这里的问题是,我声明的路径不能被调用,而只能给出正常的字符串值。这是我的密码。

代码语言:javascript
复制
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;
        }
    }
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-06-29 03:22:36

只是为了展示LINQ的表现力,以及利用可用工具的能力:

代码语言:javascript
复制
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)));
}
票数 1
EN

Stack Overflow用户

发布于 2016-06-29 02:22:31

这是因为您用字符串filechf分配变量"inputhistory" + i。使用数组或列表存储输入历史记录值。

代码语言:javascript
复制
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);

然后可以通过索引访问它的值:

代码语言:javascript
复制
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;
            }
        }
票数 1
EN

Stack Overflow用户

发布于 2016-06-29 03:29:51

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

  1. 可以将这些变量存储在字典中: 变量字典=新Dictionary();dictionary.Add("inputhistory1",inputhistory1);dictionary.Add("inputhistory2",inputhistory2);dictionary.Add("inputhistory3",inputhistory3);dictionary.Add("inputhistory4",inputhistory4);
  2. 或者您可以使用反射来获取更多信息,MSDN: 公共类TestClass {公共静态字符串inputhistory1 = "value1";公共静态字符串inputhistory2 = "value2";公共静态字符串inputhistory3 = "value3";公共静态字符串inputhistory4 = "value4";} var obj =新TestClass();var字段= typeof (TestClass).GetField("inputhistory1");//use as Console.WriteLine(field.GetValue(obj));
  3. 甚至可以使用开关/大小写返回变量值。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38089230

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档