简体   繁体   中英

File.Copy method

Is it posiible that the following code doesn't throw any exception and doesn't copy files?

void Copy2(string from, string to)
{
    lock (_thisLock)
    {
     if (File.Exists(from))
     {
      File.Copy(from, to, true);
      return;
     }
     Logger.Write("File does not exists");
    }
}

Customer says that the application doesn't crash and doesn't copy any file, and doesn't write log. Logger's type is Microsoft.Practices.EnterpriseLibrary.Logging.Logger .

Sure, if the file doesn't exist - (!File.Exists) - then the File.Copy call won't get reached.

Logger.Write does not count as an exception.

If, as your update suggests, there is nothing in the logs, I would double check the Logger.Write function. Is it implemented correctly? Is there an exception being thrown and handled within that method? That would be more likely than File.Copy failing without throwing an exception.

如果该文件不存在且未配置任何记录程序附加程序,则可能会发生您描述的情况-即无错误,无日志记录

looking at MSDN for File.Copy you can see that it throws quite a few exceptions. Make you that anything capturing the exception above the stack is handling that exception/error properly and not simply swallowing it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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