c# - C# Windows 应用程序文件:无权访问 File.Copy
问题描述
我在 ANN 有一个项目。首先,我想将图片克隆到每次都可以访问的本地目录,以便进行索引。目录是
@"D:\资产\"
对于图像,我使用的是 openfiledialog 并且多选处于打开状态。
对于容器,我使用的是这个 LOC:
> List<string[,]> path = new List<string[,]>();
整个代码是分开的。第一个用于浏览,第二个用于将图像复制到我第一次提到的目录中。
对于浏览按钮:
List<string[,]> path = new List<string[,]>();
private void btnBrowse_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "File Extention|*.jpg;*.jpeg;*.bmp";
ofd.Multiselect = true;
if(ofd.ShowDialog() == DialogResult.OK)
{
foreach (var filename in ofd.FileNames)
{
imageList.Images.Add(filename, new Bitmap(filename));
var saveFileName = Path.GetFileName(filename);
var saveFileDir = Path.GetDirectoryName(filename);
lvFruit.Items.Add(saveFileName, filename);
string[,] input = new string[,] { {saveFileDir, saveFileName } };
path.Add(input);
}
}
}
用于保存图片
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;
}
}
图像文件位于 D:\pic 并应复制到 D:\assets
错误是:
System.UnauthorizedAccessException: 在 System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite, Boolean checkHost) 处的 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 拒绝访问路径 'D:\pic ) 在 System.IO.File.Copy(String sourceFileName, String destFileName,Boolean overwrite) 在 Fruit_Dictionary.trainForm.btnSubmit_Click(Object sender EventArgs e) 在 D:\Fruit Dictionary\Fruit Dictionary\trainForm.cs:line 95
你知道它没有任何许可的原因是什么吗?
实际上我已经以管理员身份运行该程序,并且我已经检查了 D:\pic 中的权限并将其更改为执行所有权限。
出于这个原因,我已经在互联网上寻找和冲浪了 2 天,但我无法归档任何东西。任何帮助都将有助于我完成这个项目。
谢谢 :)
解决方案
尝试使用一个过程来复制你的文件
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 是您的情况下的图像旧路径,其称为文件名位于您的
foreach statement
本地路径为appPath
推荐阅读
- angular - Rxjs - DistinctUntilChanged() 与 keyup 事件
- r - 从 selectizeInput 中选择多个选项并为 r 中的每个选项绘制多条线
- c++ - 如何解决 C++ 中的 Boost 版本冲突?
- android - Android:在处理程序中更新当前活动 UI
- javascript - Redux-Thunk getStore() 不保留状态。返回“未定义”
- android - 如何在 Android 中传递 SOAP 请求?
- compiler-theory - 用 C 编写编译器
- mysql - SQL 将一列与另一表合并并添加新列
- python - python nltk 从外部 url 下载
- sql - 将日期与 SQL Server 中的日期进行比较