繁体   English   中英

无法从“System.IO.FileInfo”转换为字符串

[英]cannot convert from “System.IO.FileInfo” to string

  public class program
  {  
        private static void Main()
                {
                    string table = "T_CONSIGNACION";
                    string directory = @"C:\Users\Documents\Excel"; // Put your test root here.
        
                    var dir = new DirectoryInfo(directory);
                    var file= GetNewestFile(dir);
        
                    while (file!=null)
                    {
                        if (!File.Exists(file))
                        {
                            createLog("directory: " + dir + " \nfile: " + fichero + " don't exist");
                        }
                        else
                        {
                            importFile(file, table);
                        }
                    }
                 
                }
        
        
        
        public static FileInfo GetNewestFile(DirectoryInfo directory)
                {
                    return directory.EnumerateFiles("*.*", SearchOption.AllDirectories)
                        .MaxBy(f => (f == null ? DateTime.MinValue : f.LastWriteTime));
                }
        
        public static void createLog(string logMessage)
                { 
                     ....... 
               }
        
         public static string importFile(string file, string table)
                {
                 ........
               }
           }
    
    public static class EnumerableMaxMinExt
        {
            public static TSource MaxBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> selector)
            {
                return source.MaxBy(selector, Comparer<TKey>.Default);
            }
    
            public static TSource MaxBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> selector, IComparer<TKey> comparer)
            {
                using (IEnumerator<TSource> sourceIterator = source.GetEnumerator())
                {
                    if (!sourceIterator.MoveNext())
                    {
                        throw new InvalidOperationException("Sequence was empty");
                    }
    
                    TSource max = sourceIterator.Current;
                    TKey maxKey = selector(max);
    
                    while (sourceIterator.MoveNext())
                    {
                        TSource candidate = sourceIterator.Current;
                        TKey candidateProjected = selector(candidate);
    
                        if (comparer.Compare(candidateProjected, maxKey) > 0)
                        {
                            max = candidate;
                            maxKey = candidateProjected;
                        }
                    }
    
                    return max;
                }
            }
        }

在“主要”中,我试图获取最后更新的文件(excels 文件); var file中保存文件,然后在循环中:如果文件不存在,则创建一个 LOG,否则我要导入 go,但我在if (.File.Exists(file))中出现错误,无法从“转换” System.IO.FileInfo" 到字符串并发生循环。 我不知道我是否做得对。 你能帮助我吗??

File.Exists(...)接受文件名作为参数,而不是FileInfo object。 由于您已经拥有FileInfo ,因此您可以使用它来查看文件是否存在。 它有一个属性Exists ,所以只需使用它:

if (!file.Exists)

或者,如果您真的想使用File.Exists(...) ,您可以从文件信息中获取文件名。 这更冗长,并且可能效率较低,但我将其作为一个选项呈现:

if (!File.Exists(file.FullPath))

至于为什么你的循环永远不会终止,那是因为你永远不会重新分配file变量。 您可能需要添加file= GetNewestFile(dir); 在循环内部,但我觉得你有更多的逻辑错误。 我希望 function GetNewestFile始终返回该目录中的相同文件,对吗? 如果是这样的话,它仍然会永远循环。

好像:

var file= GetNewestFile(dir);

返回 Fileinfo object 而不是 File.Exists() 所期望的字符串。 尝试类似:

!File.Exists(file.name)

暂无
暂无

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

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