C# - 移动文件,拒绝访问? [英] C# - Moving a File, Access denied?

查看:72
本文介绍了C# - 移动文件,拒绝访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码



this is my code

private void Form1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string filepath = openFileDialog1.FileName;
FileInfo file = new FileInfo(filepath);
string name = file.Name;

                string sourceFile = @openFileDialog1.FileName;
                string destinationFile = @"C:\Program Files\Windows Sidebar\Gadgets\" + name;

                // To move a file or folder to a new location:
                System.IO.File.Move(sourceFile, destinationFile);

               }

        }





所以当我运行时我收到此错误





so when I run this I get this error

An unhandled exception of type ''System.UnauthorizedAccessException'' occurred in mscorlib.dll<br />
<br />
Additional information: Access to the path is denied.





然后它没有任何剂量。它的意思是,获取zip文件,拖出zip文件中的所有内容然后将这些内容移动到windows侧栏小工具文件夹,有人可以解释为什么这个简单的东西不起作用吗?



and then it dose nothing. What its meant to do is, get the zip file, drag out all the stuff thats in the zip file and then move those contects to the windows side bar gadgets folder, can some one please explain why this simple thing is not working?

推荐答案

试试这个



try this

private void button1_Click(object sender, EventArgs e)
     {
         OpenFileDialog ofd=new OpenFileDialog();
         if (ofd.ShowDialog()==DialogResult.OK)
         {
             System.IO.File.Move(ofd.FileName, @"Your Destination Path Here"+ofd.SafeFileName);
         }
     }


查看异常,您可能看不到任何一个来源的访问权限路径或目标路径。请检查路径是否有效,然后是否可以手动将内容复制到指定的目标路径!



如果路径有效且仍然遇到此问题,我建议你参考下面的代码 - 它将所有文件的访问权限设置为正常;然后继续你的副本。



//用于检查目标文件夹是否已包含任何文件/同名文件



foreach(Directory.GetFiles中的字符串newPath(< destination directory =>,*。*,SearchOption.AllDirectories))

{

var file = new FileInfo(newPath);

file.Attributes = FileAttributes.Normal;

File.Delete(newPath); //如果你想删除现有的文件..否则不需要。

}
Looking at the exception, it looks like you may not have access rights to either the source path or destination path. Please check if the paths are valid, and then if you can manually copy contents to the specified destination path!

If the paths are valid and still you face this problem, I suggest you to refer to the below code- where it sets the access permissions of all files to normal; and then proceed with your copy.

//For checking if the destination folder already contains any files/ a file with same name

foreach (string newPath in Directory.GetFiles(<destination directory=""> , "*.*",SearchOption.AllDirectories))
{
var file = new FileInfo(newPath);
file.Attributes = FileAttributes.Normal;
File.Delete(newPath); // if you want to delete the existing file.. else not req.
}


我们真的缺少专家吗?

您要写入的文件夹是 C:\Program Files 的子文件夹 - 这意味着(从Vista开始)普通用户一定不能在该文件夹中写入任何内容!甚至管理员也必须以更高的权限运行该程序。
Do we really lack "experts" here?
That folder you want to write to is a sub-folder of C:\Program Files - and that means (starting from Vista) that a normal user must not write anything into that folder! And even an administrator has to run the program with elevated rights to do so.


这篇关于C# - 移动文件,拒绝访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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