我有以下代码
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.
为什么会这样?
答案 0 :(得分:3)
问题在于:
file.Replace(" ", "").Name.IndexOf(dfFileWildCard.Replace(" ", ""), StringComparison.OrdinalIgnoreCase) > -1
应该是:
file.Name.Replace(" ", "").IndexOf(dfFileWildCard.Replace(" ", ""), StringComparison.OrdinalIgnoreCase) > -1
请注意,我在Replace
上呼叫file.Name
。
答案 1 :(得分:2)
我很确定它在这个代码块中:
if (
file.Replace(" ", "").Name.IndexOf(dfFileWildCard.Replace(" ", ""), StringComparison.OrdinalIgnoreCase) > -1
&&
file.LastWriteTime.Date == DateTime.Now.Date
)
由于你正在执行file.Replace(...),你正在调用this方法,它实际上替换了文件的内容,在这种情况下,创建了一个备份文件,该文件将因指定的备份文件而失败在“”中。我想你正在尝试读取文件的名称?如果是这种情况,那么您可能需要以下内容。
if (
file.Name.Replace(" ", "").Name.IndexOf(dfFileWildCard.Replace(" ", ""), StringComparison.OrdinalIgnoreCase) > -1
&&
file.LastWriteTime.Date == DateTime.Now.Date
)