2016-12-27 120 views
0

我工作的一個項目,是通過提取的文件夾中的文件解壓縮文件夾,循環並上傳數據到數據庫中,同時移動壓縮和文件夾到另一個目錄。我遇到了移動解壓縮文件夾的問題。訪問路徑被拒絕 - C#Directory.Move

Message "Access to the path 'Insurance_Documents\\Test_2017' is denied." string 

我最初的直覺是它是一個權限問題。但是,我檢查了許可,並且一切看起來不錯;另外,當程序創建目錄本身時,權限似乎並不合乎邏輯。

接下來,在環顧了一下互聯網後,我想我的uploadReportData()函數可能已經鎖定了文件(它確實利用了使用語句,但我認爲文件可能仍然存在「滯後」仍然鎖定)。作爲迴應,我將Thread.sleep語句放入代碼中,但沒有成功。該程序移動Zip文件夾就好了;這是「正常」的目錄,給我帶來麻煩。

的Program.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using UploadInsurance.Helpers; 

namespace UploadInsurance 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      try 
      { 
       ZipHelper.processDirectory(); 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine(ex.Message + "\n" + ex.StackTrace); 
      } 
      finally { 
       Console.Read(); 

      } 

     } 
    } 
} 

ZipHelper.cs(局部的)

 static class ZipHelper 
    { 
     private static String BASE_DIRECTORY = @"Insurance_Documents\"; 
     private static String OLD_ZIPS = @"Insurance_Documents\Old\"; 
     private static String EXTRACTED_FOLDERS = @"Insurance_Documents\Inserted\"; 

     private static List<String> zipFileList = new List<string>(); 


     private static void getZipFiles() 
     { 
      zipFileList = Directory.GetFiles(BASE_DIRECTORY, "*.zip").ToList(); 

     } 

     private static void processZipFiles() 
     { 
      String zipFolderName = ""; 
      String reportFolderName = ""; 


     foreach (String file in zipFileList) 
     { 
      folderName = Path.GetFileNameWithoutExtension(file); 
      reportFolderPath= BASE_DIRECTORY + folderName; 
      ZipFile.ExtractToDirectory(file, reportFolderPath); 

      uploadReportData(reportFolderPath);    
      Directory.Move(file, OLD_ZIPS + Path.GetFileName(file)); 
      Thread.Sleep(2000); 
      Directory.Move(reportFolderPath + "//", EXTRACTED_FOLDERS + folderName); 
     } 
    } 
... // MORE CODE HERE, INCLUDE uploadReportData function .. /// 

    public static void processDirectory() 
     { 
      ZipHelper.getZipFiles(); 
      ZipHelper.processZipFiles();   

     } 

我還試圖改變

Directory.Move(reportFolderPath, EXTRACTED_FOLDERS + folderName); 

Directory.Move(reportFolderPath + "//", EXTRACTED_FOLDERS + folderName); 

但仍收到錯誤。我嘗試下載並使用ProcessExplorer(如此處另一個答案中所述),訪問該目錄的唯一過程就是我的程序本身。

任何幫助將不勝感激。

編輯

我的道歉長度,但我懷疑問題可能在於下面的代碼:

private static void uploadReportData(String folderPath) 
    { 
     String empFile = ""; 
     String reportFile = ""; 
     String spouseFile = ""; 
     String childrenFile = ""; 
     String beneficiaryFile = ""; 
     String visionDependentFile = ""; 

     Boolean hasEmployeeFile = false; 
     Boolean hasReportFile = false; 

     foreach (String files in Directory.GetFiles(folderPath).ToList()) 
     { 
      if (files.Contains("employee")) 
      { 
       hasEmployeeFile = true; 
       empFile = files; 
      } 

      if (files.Contains("report")) 
      { 
       hasReportFile = true; 
       reportFile = files; 
      } 

      if (files.Contains("spouse")) 
      { 
       spouseFile = files; 
      } 

      if (files.Contains("children")) 
      { 
       childrenFile = files; 
      } 

      if (files.Contains("beneficiaries")) 
      { 
       beneficiaryFile = files; 
      } 

      if (files.Contains("vision")) 
      { 
       visionDependentFile = files; 
      } 

     } 

     String employee; 
     String report; 
     String vision; 
     String beneficiary; 
     String children; 
     String spouse; 
     CsvFileReader reader; 
     try 
     { 
      using (InsuranceModel dbContext = new InsuranceModel()) 
      { 
       EmployeeReportData empData = new EmployeeReportData(); 
       Employee emp; 
       Report newReport = new Report(); 

       if (empFile != "") 
       { 
        report = reportFile; 
        employee = empFile; 
        employee.Trim(); 
        reader = new CsvFileReader(employee); 

        List<String> employees = new List<string>(); 
        while (reader.ReadRow(employees)) { } 
        String employeeID = employees[0]; 
        emp = dbContext.Employees.FirstOrDefault(em => em.EmployeeID == employeeID); 

        // see if employee exists in the database 
        if (emp == null) 
        { 
         emp = new Employee(); 
         emp.EmployeeID = employeeID; 
         emp.SSN = employees[1]; 
         emp.FirstName = employees[2]; 
         emp.LastName = employees[3]; 
         emp.DOB = Convert.ToDateTime(employees[4]); 
         dbContext.Employees.Add(emp); 
        } 

        List<String> reportList = new List<string>(); 
        reader = new CsvFileReader(report); 
        while (reader.ReadRow(reportList)) { } 

        newReport.Employee = emp; 
        newReport.EmployeeID = emp.EmployeeID; 
        newReport.Year = reportList[1]; 
        newReport.DateSubmitted = Convert.ToDateTime(reportList[2]); 
        newReport.Action = Convert.ToInt32(reportList[3]); 
        dbContext.Reports.Add(newReport); 


        // add employees year specific data regardless 

        empData.EmployeeFirst = employees[2]; 
        empData.EmployeeLast = employees[3]; 
        empData.EmployeeGender = employees[5]; 
        empData.EmployeeEmail = employees[6]; 
        empData.EmployeeStreet = employees[7]; 
        empData.EmployeeCity = employees[8]; 
        empData.EmployeeState = employees[9]; 
        empData.EmployeeZip = employees[10]; 

        String locCode = employees[11].Trim(); 
        Location loc = dbContext.Locations.First(l => l.LocationCode == locCode); 
        empData.EmployeeLocation = loc.LocationID; 
        empData.EmployeePhone = employees[12]; 
        empData.InsurancePlan = Convert.ToInt32(employees[13]); 
        empData.VisionPlan = Convert.ToInt32(employees[14]); 
        empData.Status = employees[15].Trim(); 
        empData.Report = newReport; 

        dbContext.EmployeeReportDatas.Add(empData); 

       } 

       if (childrenFile != "") 
       { 
        children = childrenFile; 

        reader = new CsvFileReader(children); 
        List<String> childrenList = new List<string>(); 

        while (reader.ReadRow(childrenList)) 
        { 
         ChildReportData newChild = new ChildReportData(); 

         newChild.Report = newReport; 
         newChild.ChildFirst = childrenList[0]; 
         newChild.ChildLast = childrenList[1]; 
         newChild.ChildDOB = Convert.ToDateTime(childrenList[2]); 
         newChild.ChildGender = childrenList[3]; 
         newChild.ChildStreet = childrenList[4]; 
         newChild.ChildCity = childrenList[5]; 
         newChild.ChildState = childrenList[6]; 
         newChild.ChildZip = childrenList[7]; 
         newChild.Step = childrenList[8]; 
         newChild.Foster = childrenList[9]; 
         newChild.Student = childrenList[10]; 
         newChild.Handicap = childrenList[11]; 
         newChild.ChildSSN = childrenList[12]; 

         dbContext.ChildReportDatas.Add(newChild); 

         childrenList.Clear(); // clear in preparation for reading a new row 

        } 


       } 

       if (spouseFile != "") 
       { 

        spouse = spouseFile; 
        reader = new CsvFileReader(spouse); 
        List<String> spouseList = new List<string>(); 

        while (reader.ReadRow(spouseList)) { } 
        SpouseReportData newSpouse = new SpouseReportData(); 

        newSpouse.Report = newReport; 
        newSpouse.SpouseSSN = spouseList[0]; 
        newSpouse.SpouseFirst = spouseList[1]; 
        newSpouse.SpouseLast = spouseList[2]; 
        newSpouse.SpouseStreet = spouseList[3]; 
        newSpouse.SpouseCity = spouseList[4]; 
        newSpouse.SpouseState = spouseList[5]; 
        newSpouse.SpouseZip = spouseList[6]; 
        newSpouse.SpouseGender = spouseList[7]; 
        newSpouse.SpouseDOB = Convert.ToDateTime(spouseList[8]); 
        newSpouse.SpouseEmployed = spouseList[9]; 

        dbContext.SpouseReportDatas.Add(newSpouse); 



       } 

       if (beneficiaryFile != "") 
       { 
        beneficiary = beneficiaryFile; 
        reader = new CsvFileReader(beneficiary); 
        List<String> beneficiaryList = new List<string>(); 

        while (reader.ReadRow(beneficiaryList)) 
        { 
         BeneficiaryReportData newBeneficiary = new BeneficiaryReportData(); 

         newBeneficiary.Report = newReport; 
         newBeneficiary.BeneficiarySSN = beneficiaryList[0]; 
         newBeneficiary.BeneficiaryFirst = beneficiaryList[1]; 
         newBeneficiary.BeneficiaryLast = beneficiaryList[2]; 
         newBeneficiary.BeneficiaryStreet = beneficiaryList[3]; 
         newBeneficiary.BeneficiaryCity = beneficiaryList[4]; 
         newBeneficiary.BeneficiaryState = beneficiaryList[5]; 
         newBeneficiary.BeneficiaryZip = beneficiaryList[6]; 
         newBeneficiary.BeneficiaryPercentage = Convert.ToDecimal(beneficiaryList[7]); 
         newBeneficiary.BeneficiaryRelationship = beneficiaryList[8]; 
         newBeneficiary.BeneficiaryType = beneficiaryList[9]; 

         dbContext.BeneficiaryReportDatas.Add(newBeneficiary); 
         beneficiaryList.Clear(); // clear in preparation for reading a new row 

        } 



       } 

       if (visionDependentFile != "") 
       { 

        vision = visionDependentFile; 
        reader = new CsvFileReader(vision); 
        List<String> visionList = new List<string>(); 

        while (reader.ReadRow(visionList)) 
        { 
         VisionDependentReportData newVision = new VisionDependentReportData(); 

         newVision.Report = newReport; 
         newVision.VisionSSN = visionList[0]; 
         newVision.VisionFirst = visionList[1]; 
         newVision.VisionLast = visionList[2]; 
         newVision.VisionDOB = Convert.ToDateTime(visionList[3]); 
         newVision.VisionGender = visionList[4]; 
         newVision.VisionRelationship = visionList[5]; 

         dbContext.VisionDependentReportDatas.Add(newVision); 
         visionList.Clear(); // clear in preparation for reading a new row 

        } 

       } 


       dbContext.SaveChanges(); 

      } 


     } 
+0

如果您運行的帳戶下的程序,而是嘗試創建與代碼的文件夾,並手動做運動,而程序仍在運行。如果你不能手動移動它,那麼你自己的程序可能會鎖定一個文件或其他東西。 – CodingYoshi

+0

感謝您的建議。這確實是我的程序,但我不確定代碼的哪一部分將其鎖定。我會發布我的uploadReportData函數,因爲我懷疑它可能是。非常感謝。 – KellyMarchewa

+1

這可能是通過'using'使用CsvFileReader,像'使用(CsvFileReader讀卡器=新CsvFileReader(兒童))是一個好主意 {...}',以確保文件句柄被釋放的使用條款之外。 –

回答

1

我的直覺說,你需要關閉/處置CsvFileReader對象。任何要歸檔的流都會鎖定,直到您釋放它。

+0

你和Alex J絕對正確。對不起,當答案如此明顯時,我不得不問這個問題。我沒有寫CsvFileReader類。獲得的教訓 - 使用第三方代碼時,請確保您瞭解如何正確使用它。班級絕對沒有錯(非常感謝提供者和所有爲其他人提供此類代碼的人)。我想,當「結束」聲明結束時,我認爲它關閉/處理了資源。再次感謝。 – KellyMarchewa