-1

我有以下代码

            string[] dfFileWildCards = Helper.DataFeedsWildCards.Split(',');
            string[] dfOriginalFilesPath = Directory.GetFiles(Helper.DataFeedsPath).Where(file => new FileInfo(file).CreationTime.Date == DateTime.Now.Date).ToArray();

            Helper.MyWriteLine("-->DataFeeds Folder: " + Helper.DataFeedsPath);
            Helper.MyWriteLine("-->WildCards: " + Helper.DataFeedsWildCards);
            Helper.MyWriteLine("-->There are " + dfOriginalFilesPath.Length + " files");

            foreach (string filePath in dfOriginalFilesPath)
            {
                Helper.MyWriteLine("Checking " + filePath.ToLower());
                FileInfo file = new FileInfo(filePath.ToLower());

                foreach (string dfFileWildCard in dfFileWildCards)
                {
                    if (
                        file.Replace(" ", "").Name.IndexOf(dfFileWildCard.Replace(" ", ""), StringComparison.OrdinalIgnoreCase) > -1
                        &&
                        file.LastWriteTime.Date == DateTime.Now.Date
                        )
                    {
                        filesToCopy.Add(filePath.ToLower());
                        Helper.MyWriteLine("---->Marked for copy " + file.Name + ".");
                    }
                }
            }

它在 d:\cnbrd\datafeeds\authorizeddatafeedfile_20121127.txt 的日志中显示错误

11/28/2012 1:27:53 AM -->DataFeeds Folder: D:\CNBRD\DataFeeds
11/28/2012 1:27:53 AM -->WildCards: Inactive
11/28/2012 1:27:53 AM -->There are 7 files
11/28/2012 1:27:53 AM Checking d:\cnbrd\datafeeds\authorizeddatafeedfile_20121127.txt
11/28/2012 1:27:53 AM Exception occured: The path is not of a legal form.

为什么会这样?

4

2 回答 2

3

问题在这里:

file.Replace(" ", "").Name.IndexOf(dfFileWildCard.Replace(" ", ""), StringComparison.OrdinalIgnoreCase) > -1

应该是:

file.Name.Replace(" ", "").IndexOf(dfFileWildCard.Replace(" ", ""), StringComparison.OrdinalIgnoreCase) > -1

注意我正在打电话Replacefile.Name

于 2012-11-28T06:49:30.313 回答
2

我很确定它在这个代码块中:

if (
    file.Replace(" ", "").Name.IndexOf(dfFileWildCard.Replace(" ", ""), StringComparison.OrdinalIgnoreCase) > -1
    &&
    file.LastWriteTime.Date == DateTime.Now.Date
    )

由于您正在执行 file.Replace(...) 您正在调用方法,它实际上替换了文件的内容,并且在这种情况下,创建了一个备份文件,由于“”中指定的备份文件,该备份文件将失败。我认为您正在尝试读取文件名?如果是这种情况,那么您可能需要以下内容。

if (
    file.Name.Replace(" ", "").Name.IndexOf(dfFileWildCard.Replace(" ", ""), StringComparison.OrdinalIgnoreCase) > -1
    &&
    file.LastWriteTime.Date == DateTime.Now.Date
    )
于 2012-11-28T06:50:06.600 回答