【问题标题】:Folder Copy Error:System.UnauthorizedAccessException in C#文件夹复制错误:C# 中的 System.UnauthorizedAccessException
【发布时间】:2020-01-27 08:48:34
【问题描述】:

我想将现有文件复制到另一个目的地,但它给出了访问错误。

Error Name:
System.UnauthorizedAccessException

我知道我需要授予访问权限,但我不知道该怎么做。

我的项目如下所示:

我会将文件放在服务器上并从基于 Web 的程序中运行该程序。我将使用源文件关闭按钮和文本框。我只会让目标表单保持打开状态

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        BackgroundWorker worker = new BackgroundWorker();
        public Form1()
        {
            InitializeComponent();
            worker.WorkerReportsProgress = true;
            worker.WorkerSupportsCancellation = true;

            worker.ProgressChanged += Worker_ProgressChanged;
            worker.DoWork += Worker_DoWork;

        }

        void Copyfile(string source, string des)
        {
            FileStream fsOut = new FileStream(des, FileMode.Create);
            FileStream fsIn = new FileStream(source, FileMode.Open);
            byte[] bt = new byte[1048456];
            int readByte;
            while ((readByte = fsIn.Read(bt, 0, bt.Length)) > 0)
            {
                fsOut.Write(bt,0,readByte);
                worker.ReportProgress((int)(fsIn.Position * 100 / fsIn.Length));
            }
            fsIn.Close();
            fsOut.Close();
        }
        private void Worker_DoWork(object sender, DoWorkEventArgs e)
        {
            Copyfile(txtSource.Text, txtTarget.Text);
        }
        private void Worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
            label1.Text = progressBar1.Value.ToString() + "%";
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {

            FolderBrowserDialog fbd1 = new FolderBrowserDialog();
            if (fbd1.ShowDialog() == DialogResult.OK)
            {
                txtSource.Text = Path.Combine(fbd1.SelectedPath, Path.GetFileName(txtTarget.Text));
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            worker.RunWorkerAsync();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                txtTarget.Text = Path.Combine(fbd.SelectedPath, Path.GetFileName(txtSource.Text));
            }
        }
    }
}

【问题讨论】:

  • 您是否尝试以管理员身份运行
  • 我假设一定有不使用 File.Copy() 的原因?
  • 我在 app.manifest 文件上将 level="asInvoker" uiAccess="false" 更改为 level="requireAdministrator" uiAccess="false" 但仍然无法正常工作
  • 还有 3 件事要尝试 1. 右键单击​​文件夹并检查安全选项卡并验证权限,修改您的应用程序在其下运行的用户的权限 2. 如果您使用 IIS,请检查 IIS 是否具有权限正确 3.检查并设置文件属性 示例:File.SetAttributes(file, FileAttributes.Normal);
  • @Clint 我写了一个文件(很抱歉),但我正在尝试复制一个文件夹和该文件夹的所有子目录

标签: c# .net frameworks


【解决方案1】:

我添加并更改了一些代码

public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
        {
            Directory.CreateDirectory(target.FullName);
            // HER DOSYAYI YENI DIZINE KOPYALAR
            foreach (FileInfo fi in source.GetFiles())
            {
                fi.CopyTo(System.IO.Path.Combine(target.FullName, fi.Name), true);
            }
            // HER ALT DIZINI KOPYALAR
            foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
            {
                DirectoryInfo nextTargetSubDir =
                target.CreateSubdirectory(diSourceSubDir.Name);
                CopyAll(diSourceSubDir, nextTargetSubDir);
            }
        }

        void CopyFile(string source, string des)
        {
            FileStream fsout = new FileStream(des, FileMode.Create);
            FileStream fsIn = new FileStream(source, FileMode.Open);
            byte[] bt = new byte[1048756];
            int readByte;
            while ((readByte=fsIn.Read(bt,  0,  bt.Length))>0)
            {
                fsout.Write(bt, 0, readByte);
                worker.ReportProgress((int)(fsIn.Position*100/fsIn.Length));
            }
            fsIn.Close();
            fsout.Close();
        }
        private void Worker_DoWork(object sender, DoWorkEventArgs e)
        {
            CopyAll(new DirectoryInfo( txtSource.Text), new DirectoryInfo(txtTarget.Text));
        }

我添加我的 github 存储库:https://github.com/yusufcelik1/Copy-Folder.git

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-07
    • 2015-08-19
    • 2011-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多