Folder Copy Error:System.UnauthorizedAccessException in C#

Folder Copy Error:System.UnauthorizedAccessException in C#

我想将现有文件复制到另一个目标位置,但出现访问错误。

Error Name:
System.UnauthorizedAccessException

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

我的项目如下所示:

我会将文件放在服务器上,运行 这个程序来自基于网络的程序。我将关闭带有源文件的按钮和文本框。我只会让目标表格保持打开状态

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));
            }
        }
    }
}

我添加和更改了一些代码

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