C#Windows应用程序文件:无法访问文件。复制

C#Windows应用程序文件:无法访问文件。复制,c#,system.io.file,C#,System.io.file,我在安有一个项目。首先,我想将图片克隆到本地目录,该目录每次都可以访问,以便建立索引。目录是 @“D:\assets\” 对于图像,我使用openfiledialog,并且multiselect处于启用状态 对于容器,我使用以下LOC: > List<string[,]> path = new List<string[,]>(); 图像文件位于D:\pic,应复制到D:\assets 错误是: System.UnauthorizedAccessException:

我在安有一个项目。首先,我想将图片克隆到本地目录,该目录每次都可以访问,以便建立索引。目录是

@“D:\assets\”

对于图像,我使用openfiledialog,并且multiselect处于启用状态

对于容器,我使用以下LOC:

> List<string[,]> path = new List<string[,]>();
图像文件位于D:\pic,应复制到D:\assets

错误是:

System.UnauthorizedAccessException:对路径“D:\pic”的访问被拒绝 在System.IO.\uuu Error.WinIOError(Int32 errorCode,字符串maybeFullPath) 位于System.IO.File.InternalCopy(字符串sourceFileName、字符串destFileName、布尔覆盖、布尔检查主机) 在System.IO.File.Copy处(字符串sourceFileName、字符串destFileName、布尔覆盖) 在Fruit\u Dictionary.trainForm.btnSubmit\u中,单击D:\Fruit Dictionary\Fruit Dictionary\trainForm.cs:第95行中的(对象发送者事件参数e)

你知道它没有许可证的原因吗

实际上我已经以管理员的身份运行了这个程序,ofc我已经检查了D:\pic中的权限,并将其更改为“执行所有权限”

正因为这个原因,我已经在网上搜索和冲浪两天了,但我什么都做不了。任何帮助都将有助于我完成这个项目


谢谢:)

尝试使用流程复制您的文件

             foreach(String ImageOldPath in Paths)
             {
               try{
                ProcessStartInfo info = new ProcessStartInfo();
                info.FileName = "C:\\Windows\\system32\\xcopy.exe";
                info.Arguments = $"{ImageOldPath LocalPath} "
                Console.WriteLine(info.Arguments);
                info.UseShellExecute = false;
                info.CreateNoWindow = true;
                info.WindowStyle = ProcessWindowStyle.Hidden;
                Process proc = new Process();
                proc.StartInfo = info;
                proc.Start();
                proc.Dispose();
                }catch(Exception e){}
             }
更新

  • ImageOldPath是本例中的图像旧路径,其名为filename,位于
    foreach语句中

  • 本地路径为appPath


新位图(文件名)
导致此问题。位图类在引擎盖下使用内存映射文件。在调用其Dispose()方法之前,MMF会一直锁定该文件。一般来说,非常非常重要,因为位图也往往会使用大量非托管内存,并且很容易使用OutOfMemoryException使程序崩溃。使用ImageList并不是一个好主意,但这很容易,您可以在将位图添加到列表后立即处理它。@HansPassant-hmm。。实际上对于这段代码,imageList.Images.Add(文件名,新位图(文件名));它只是为了将文件显示在listview中,您可以看到它正在工作。。只是为了“外表”。。不用于复制文件。我建议尽可能始终使用
using
关键字,在您的情况下,直接怀疑的是位图,因此您可以使用
using(var bmp=new bitmap(filename))
@styx:“using”是什么意思?嗯,那一个没问题。但问题不同。@mjwills ah oke2。。因此File.Copy的值(iPath,appPath+iName,true);是File.Copy(D:\pic,D:\assets\+a.jpg,true);所以我应该把foreach放在foreach里面?
    private void btnSubmit_Click(object sender, EventArgs e)
    {
        String fruitname = txtFruitName.Text;
        string appPath = @"D:\assets\";
        if (txtFruitName.Text == "")
        {
            MessageBox.Show("Fruit Name Should be Filled");
        }
        else
        {
            int newindex = 0;

            foreach (var item in path)
            {
                newindex += 1;
                string iName = item[newindex - 1, 1].Trim();
                string iPath = item[newindex - 1, 0].Trim();
                try
                {
                    File.Copy(iPath, appPath + iName, true);
                    File.SetAttributes(appPath, FileAttributes.Normal);
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.ToString());
                    //MessageBox.Show("Permission error or picture is already exists");
                }
                path.Clear();
                }

            MessageBox.Show("All Picture Has Been Saved");
            lvFruit.Clear();
            txtFruitName.Text = "";
            btnDone.Enabled = true;
        }
    }
             foreach(String ImageOldPath in Paths)
             {
               try{
                ProcessStartInfo info = new ProcessStartInfo();
                info.FileName = "C:\\Windows\\system32\\xcopy.exe";
                info.Arguments = $"{ImageOldPath LocalPath} "
                Console.WriteLine(info.Arguments);
                info.UseShellExecute = false;
                info.CreateNoWindow = true;
                info.WindowStyle = ProcessWindowStyle.Hidden;
                Process proc = new Process();
                proc.StartInfo = info;
                proc.Start();
                proc.Dispose();
                }catch(Exception e){}
             }