将文件从UNC复制到本地驱动器 [英] Copy Files from UNC to Local Drive

查看:119
本文介绍了将文件从UNC复制到本地驱动器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我只是C#的初学者,几乎没有帮助会更好.我需要从网络中的其他计算机复​​制一个文件,路径为UNC,如\\ something \ files \ index.txt

但是在复制这些文件之前,我需要检查用户是否有权访问文件夹.如果他没有阅读权限,我想显示一条消息.
否则,将所有文件复制到c:/something

Hello,

I am just beginner in C# and little help will be more then fine. I need to copy one file from other computer in network, path is UNC like \\something\files\index.txt

But before I make copy of those files I need to check that user have access right to folder. If he doesn’t have read permissions I will like to show message.
Otherwise copy all files to c:/something

推荐答案

User System.IO.File.Copy方法,它将处理所有有问题的情况.如果用户无权访问该文件,则该方法将引发UnauthorizedAccessException.

User System.IO.File.Copy method, it will handle all problematic cases. The method throws UnauthorizedAccessException if user doesn''t have access to the file.

try
{
    File.Copy(sourceFile, destination);
}
catch (UnauthorizedAccessException ex)
{
    // this exception is thrown if you don't have permissions
}
catch (Exception ex)
{
    // other problems
}


示例-请参见此处此处.


这篇关于将文件从UNC复制到本地驱动器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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