问题描述
静态方法 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 :
<$c$c>UnauthorizedAccessException$c$c>
- 在没有所要求的权限调用者。
<$c$c>ArgumentException$c$c>
-
sourceFileName
或destFileName
是一个零长度字符串,仅包含空白或者包含一个或者通过<$c$c>InvalidPathChars$c$c>.
sourceFileName
ordestFileName
is a zero-length string, contains only white space, or contains one or more invalid characters as defined byInvalidPathChars
.
- 或 -
sourceFileName
或 destFileName
指定的目录中。
<$c$c>ArgumentNullException$c$c>
- sourceFileName或destFileName为空。
<$c$c>PathTooLongException$c$c>
- 在指定的路径,文件名或者两者都超出了系统定义的最大长度。例如,在基于Windows的平台上,路径必须小于248个字符,文件名必须小于260个字符。
<$c$c>DirectoryNotFoundException$c$c>
- 在sourceFileName或destFileName指定的路径无效(例如,它位于未映射的驱动器上)。
<$c$c>FileNotFoundException$c$c>
-
sourceFileName
未找到。
sourceFileName
was not found.
<$c$c>IOException$c$c>
-
destFileName
的存在。
- 或 -
发生I / O错误。
<$c$c>NotSupportedException$c$c>
- sourceFileName <$ C C $>或
destFileName
是一个无效的格式。
sourceFileName
ordestFileName
is in an invalid format.
这篇关于如何知道File.Copy成功了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!