简体   繁体   English

关于文件的FileInfo抛出错误

[英]FileInfo throwing error regarding file

I have the following code 我有以下代码

            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 + ".");
                    }
                }
            }

It is showing error in log for d:\\cnbrd\\datafeeds\\authorizeddatafeedfile_20121127.txt 它在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.

Why is it so ? 为什么会这样呢?

The problem is here: 问题在这里:

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

Should probably be: 应该可能是:

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

Notice I'm calling Replace on file.Name . 注意,我在file.Name上调用Replace

I'm pretty sure it is in this code block: 我敢肯定它是在这个代码块:

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

Since you are doing file.Replace(...) you are calling this method, which actually replaces contents of files, and in this case, creating a backup file, which will fail since the specified backup file in "". 由于您正在执行file.Replace(...),因此您将调用方法, 方法实际上将替换文件的内容,在这种情况下,将创建一个备份文件,由于指定的备份文件位于“”中,因此该备份文件将失败。 I think you are trying to read the name of the file? 我认为您正在尝试读取文件名? If that is the case, then the following might be what you need. 如果是这种情况,那么您可能需要以下内容。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM