我工作的一個項目,被存放在存儲器中的一個區域的一系列圖像(從記憶棒或下載說了),並且將它們分發出去他們各自的文件夾基於圖像的名稱 - 這應該與其各自文件夾中的文件的名稱相對應。C# - 到路徑訪問被拒絕,無法訪問文件
我寫要做到這一點,並決定通過改變代碼來測試它,這樣它會在圖片收藏文件夾中的文件的集合進行處理的代碼的前幾個功能。 應該發生的事情是,該文件夾中的每個文件都被複制到AppData中名爲「新圖像」的文件夾中,並添加到適當的子目錄中或根據需要創建子目錄。
這投擲了一個錯誤,指出訪問C:\Users\mark
雖然它沒有解釋爲什麼還是做什麼了。 我認爲這可能是訪問我的圖片文件夾的問題,所以我將這些圖像複製到AppData文件夾內名爲'Test Images'的文件夾中(所以程序現在只是在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 – marcuthh