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

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

问题描述

静态方法 File.Copy(String,String)不返回值。如果该功能成功,我如何以编程方式知道?如果没有抛出异常, 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 :


UnauthorizedAccessException


  • 呼叫者没有必要的权限。 / li>
  • The caller does not have the required permission.

ArgumentException


  • sourceFileName destFileName 是一个零长度字符串,仅包含空格,或包含一个或多个无效字符,由< a href =http://msdn.microsoft.com/en-us/library/system.io.path.invalidpathchars.aspx =noreferrer> 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 指定一个目录。

ArgumentNullException


  • sourceFileName或destFileName为null。

PathTooLongException


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

DirectoryNotFoundException


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

FileNotFoundException


  • 没有找到sourceFileName

  • sourceFileName was not found.

IOException


  • destFileName 存在

发生I / O错误。

NotSupportedException


  • sourceFileName destFileName 的格式无效。

  • sourceFileName or destFileName is in an invalid format.

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

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