Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 文件夹复制错误:C中的System.UnauthorizedAccessException#_C#_.net_Frameworks - Fatal编程技术网

C# 文件夹复制错误:C中的System.UnauthorizedAccessException#

C# 文件夹复制错误:C中的System.UnauthorizedAccessException#,c#,.net,frameworks,C#,.net,Frameworks,我想将现有文件复制到另一个目标,但会出现访问错误 Error Name: System.UnauthorizedAccessException 我知道我需要授予访问权限,但我不知道如何做到这一点 我的项目看起来像: 我将把文件放在服务器上,并从基于web的程序运行此程序。我将关闭带有源文件的按钮和文本框。我只会让目标表单保持打开状态 using System; using System.Collections.Generic; using System.ComponentModel; usi

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

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


我添加并更改了一些代码

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存储库:

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