【发布时间】:2015-09-22 10:30:13
【问题描述】:
我正在开展一个项目,该项目将一系列图像存储在内存的一个区域(例如从记忆棒或下载)中,并根据图像的名称将它们分发到各自的文件夹中 - 这应与各自文件夹中的文件名相对应。
我编写了代码的前几个函数来实现这一点,并决定通过更改代码来测试它,以便它可以对“我的图片”文件夹中的文件集合执行该过程。 应该发生的是,文件夹中的每个文件都被复制到 AppData 中名为“新图像”的文件夹中,并添加到相应的子目录中,或者在必要时创建子目录。
这引发了一个错误,指出可以访问C:\Users\mark,尽管它没有解释为什么或如何处理它。
我认为这可能是访问“我的图片”文件夹时出现的问题,因此我将图像复制到 AppData 文件夹内名为“测试图像”的文件夹中(因此程序现在只是在 AppData 中的两个文件夹之间传输文件)。发生了同样的错误,我真的不知道下一步该怎么做,我之前已经多次写入和读取 AppData 中的文件,但从未遇到过这个问题。我还在这个论坛上阅读了与此相关的各种条目,但似乎无法就下一步做什么得到明确的答案!
导致异常的代码如下:
//main folder (Contains sub-folders for each patient)
string rootDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\New Images";
//variables for sub-directories cannot be given at this point as they are created using a part of the image name
string subDirectory;
//string subDirectory = Path.Combine(rootDirectory, imageName.Split('_')[0]);
string imageName;
//string imageName = Path.GetFileName(image)
string shortcutDirectory;
//string shortcutDirectory = My Documents + Subfolder name + file name
//list to hold all strings as bitmap image
List<Bitmap> images = new List<Bitmap>();
public void createDirectory()
{
//create filing construct for all files passed in from machines
//if main folder does not exist in AppData
if (!Directory.Exists(rootDirectory))
{
//create it
Directory.CreateDirectory(rootDirectory);
}
}
public void saveLatestImages()
{
//specific path for My Pictures only
string testImagesPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Test Images";
//if there is a Pictures folder
if (Directory.Exists(testImagesPath))
{
//get number of files in folder
int fileCount = Directory.GetFiles(testImagesPath).Count();
//more than one file in folder
if (fileCount > 0)
{
//create data structures to store file info
//filePaths holds path of each file represented as a string
string[] filePaths = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Test Images");
//for each file in Pictures...
for (int index = 0; index < fileCount; ++index)
{
//get name of image at current index
imageName = filePaths[index];
//separate the part relating to the patient name (everything before (DD/MM/YYYY))
string subSpecifier = imageName.Split('_')[0];
//add to root directory to form subfolder name
subDirectory = Path.Combine(rootDirectory, subSpecifier);
//subdirectory name formulated, check for pre-existing
//subfolder does not exist
if(!Directory.Exists(subDirectory))
{
//create it
Directory.CreateDirectory(subDirectory); //ERROR OCCURS
}
//otherwise, file will be added to existing directory
//take everything from end and folder\file division to get unique filename
string fileName = imageName.Split('\\').Last();
//add this to the existing subDirectory
fileName = Path.Combine(subDirectory, fileName);
//copy the image into the subfolder using this unique filename
File.Copy(imageName, fileName);
//add full filename to list of bitmap images
images.Add(new Bitmap(fileName));
//update the shortcut to the file in the image storage shortcut folder
shortcutDirectory = getShortcut(subSpecifier, fileName);
//delete image at original path (clear folder so images not copied on next load up)
//File.Delete(imageName);
}
}
}
}
非常感谢您在下一步寻找的任何帮助!
谢谢 标记
【问题讨论】:
-
您是否添加了 require 文件夹的权限,即 C:\Users\mark?
-
我不知道,因为我说我是这个错误的新手,所以我什至不会考虑任何事前抵消它!我将如何添加权限? @AmitSoni
标签: c# access-denied