如何知道File.Copy成功了吗? [英] How to know that File.Copy succeeded?

查看:217
本文介绍了如何知道File.Copy成功了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

静态方法 File.Copy(字符串,字符串)不返回值。我怎么能知道编程如果该功能成功与否?如果没有抛出异常, File.Copy 顺利。但我问如何把也不例外作为条件。

The static method File.Copy(String, String) doesn't return a value. How can I know programatically if that function succeeded or not ? If there is no thrown exception, File.Copy goes well. But I am asking about how to put "no exception" as a condition.

事情是这样的:

if(no exception happened){

//my code goes here

}


编辑:我已经解决了用一个简单的计数器如下问题:


I have solved the problem using a simple counter as following:

int i=0;
try{
    File.Copy();
}
catch(e1){
    i++;
}
catch(e2){
    i++;
}

if(i==0){
    //my code goes here
}

感谢所有贡献者。我将通过你的答案选择最好的。

Thanks for all contributors. I will go through your answers to choose the best.

推荐答案

如果操作不抛出任何异常,就意味着它是成功的。可能的例外列表,请这里

If the operation doesn't throw any exception, it means that it was successful. The list of the possible exceptions is available here :

<一个href="http://msdn.microsoft.com/en-us/library/system.unauthorizedaccessexception.aspx"><$c$c>UnauthorizedAccessException

      
  • 在没有所要求的权限调用者。
  •   

<一个href="http://msdn.microsoft.com/en-us/library/system.argumentexception.aspx"><$c$c>ArgumentException

      
  • sourceFileName destFileName 是一个零长度字符串,仅包含空白或者包含一个或者通过<一个定义多个无效字符href="http://msdn.microsoft.com/en-us/library/system.io.path.invalidpathchars.aspx"><$c$c>InvalidPathChars.

  • sourceFileName or destFileName is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.

- 或 -

sourceFileName destFileName 指定的目录中。

<一个href="http://msdn.microsoft.com/en-us/library/system.argumentnullexception.aspx"><$c$c>ArgumentNullException

      
  • sourceFileName或destFileName为空。
  •   

<一个href="http://msdn.microsoft.com/en-us/library/system.io.pathtoolongexception.aspx"><$c$c>PathTooLongException

      
  • 在指定的路径,文件名或者两者都超出了系统定义的最大长度。例如,在基于Windows的平台上,路径必须小于248个字符,文件名必须小于260个字符。
  •   

<一个href="http://msdn.microsoft.com/en-us/library/system.io.directorynotfoundexception.aspx"><$c$c>DirectoryNotFoundException

      
  • 在sourceFileName或destFileName指定的路径无效(例如,它位于未映射的驱动器上)。
  •   

<一个href="http://msdn.microsoft.com/en-us/library/system.io.filenotfoundexception.aspx"><$c$c>FileNotFoundException

      
  • sourceFileName 未找到。
  •   
  • sourceFileName was not found.

<一个href="http://msdn.microsoft.com/en-us/library/system.io.ioexception.aspx"><$c$c>IOException

      
  • destFileName 的存在。

- 或 -

发生I / O错误。

<一个href="http://msdn.microsoft.com/en-us/library/system.notsupportedexception.aspx"><$c$c>NotSupportedException

      
  • sourceFileName <$ C C $>或 destFileName 是一个无效的格式。
  •   
  • sourceFileName or destFileName is in an invalid format.

这篇关于如何知道File.Copy成功了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆