C#代码可以找到最新的文件和类型

时间:2011-03-22 23:00:42

标签: c# file

对不起朋友......先发帖到这里。刚刚掌握这个网站。感谢迄今为止的帮助。我现在已经过了第一个错误,因为“”在那里。现在,程序运行但它没有写入App.config文件来更新文件名。

    private static bool FindAndNoteMostRecentMsl() 
    {
        // Get files from directory
        Console.WriteLine("Finding log file");
        string logsFilePath = ConfigurationManager.AppSettings["LogFilePath"];
        DirectoryInfo directory = new DirectoryInfo(logsFilePath);

        FileInfo mostRecentMslFile = (from file in directory.GetFiles()
                                      where file.Extension.ToLower() == "msl"
                                      orderby file.CreationTime descending
                                      select file).FirstOrDefault();

        // 1) update name of most recent MSL in config
        UpdateAppSettingValue("CurrentMslFileName", "mostRecentMslFile.Name");

        // If nothing found, exits the program
        if (mostRecentMslFile == null)
            Console.WriteLine("...File not found...");
            return false;

        // If most recent MSL matches value in App.config, does nothing
        string mslFileNameInConfig = ConfigurationManager.AppSettings["CurrentMslFileName"];
        if (mslFileNameInConfig == mostRecentMslFile.Name)
            return true;

        // 2) reset counter in config
        UpdateAppSettingValue("CurrentErrorCounter", "0");

        return true;
    }

1 个答案:

答案 0 :(得分:3)

你可能有错字,你想使用变量logsFilePath而不是"logsFilePath"

DirectoryInfo directory = new DirectoryInfo("logsFilePath");

-->

DirectoryInfo directory = new DirectoryInfo(logsFilePath);