2015-09-22 156 views
0

我工作的一個項目,被存放在存儲器中的一個區域的一系列圖像(從記憶棒或下載說了),並且將它們分發出去他們各自的文件夾基於圖像的名稱 - 這應該與其各自文件夾中的文件的名稱相對應。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); 
       } 
      } 
     } 
    } 

在旁邊看任何幫助將不勝感激!

感謝 馬克

+0

您是否添加了require文件夾的權限,即C:\ Users \ mark? –

+0

我不知道,因爲我說我是新來的這個錯誤,所以我甚至不會考慮任何事情來預先抵消它!我將如何去添加權限? @AmitSoni – marcuthh

回答

1

這是一個ASP.net的Web應用程序?在這種情況下,您可以嘗試向IIS_IUSRS帳戶提供寫入權限。

解決這個問題的竅門是ASP.net模擬。

不充分的權利並運行你的應用程序作爲管理員。用戶將擁有管理權限,我相信你會想避免這種情況。

+0

嗨,這聽起來不錯,非常有幫助。正如我在這個問題中所說的,我是新來的這個錯誤,並且幾乎不知道它會涉及到做什麼改變你建議。你知道可以幫助我的鏈接或漫遊嗎? – marcuthh

+0

@marcuthh。類似的討論。可能這是你在找什麼? http://stackoverflow.com/questions/14653722/how-do-i-give-asp-net-permission-to-write-to-a-folder-in-windows-7 –

+0

http://forums.iis。 net/t/1162412.aspx?IIS + setup + for + ASP + NET + write + access也可能有所幫助。 –

1

對於時間被充分權利,每個人都在您的文件夾,並嘗試運行你的應用程序作爲管理員。

如果有效,那麼您可以相應地更改文件夾的權限。還要確保你的文件夾不是隻讀的。

+0

我會嘗試這個測試階段,感謝您的輸入! – marcuthh