访问路径被拒绝 - C# Directory.Move
Posted
技术标签:
【中文标题】访问路径被拒绝 - C# Directory.Move【英文标题】:Access to Path Denied - C# Directory.Move 【发布时间】:2016-12-27 21:38:42 【问题描述】:我正在开发一个项目,该项目是解压缩文件夹,循环提取文件夹中的文件并将数据上传到数据库,然后将 zip 和文件文件夹移动到另一个目录。我在移动提取的文件夹时遇到问题。
Message "Access to the path 'Insurance_Documents\\Test_2017' is denied." string
我最初的直觉是这是一个权限问题。但是,我检查了许可,一切看起来都很好;此外,由于程序会自行创建目录,因此将权限作为原因似乎不太合乎逻辑。
接下来,在网上浏览了一下之后,我认为我的 uploadReportData() 函数可能已经锁定了文件(它确实使用了 using 语句,但我认为可能仍然存在文件仍然存在的“滞后”锁定)。作为回应,我将 Thread.sleep 语句放入我的代码中,但没有成功。程序移动 Zip 文件夹就好了;给我带来麻烦的是“正常”目录。
程序.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();
【问题讨论】:
如果您在您的帐户下运行程序,请尝试使用代码创建文件夹并在程序仍在运行时手动执行移动。如果您无法手动移动它,那么您自己的程序可能正在锁定文件或其他东西。 感谢您的建议。这确实是我的程序,但我只是不确定代码的哪一部分锁定了它。我将发布我的 uploadReportData 函数,因为我怀疑它可能是这样的。非常感谢。 通过using
使用CsvFileReader 可能是个好主意,例如using (CsvFileReader reader = new CsvFileReader(children)) ...
,以确保文件句柄在using 子句之外释放。
将代码放入try catch块中,检查异常以及内部异常和堆栈跟踪!
【参考方案1】:
我的直觉是您需要关闭/处置 CsvFileReader 对象。任何文件流都将锁定,直到您释放它。
【讨论】:
你和 Alex J 完全正确。很抱歉,当答案如此明显时,我不得不问这个问题。我没有编写 CsvFileReader 类。经验教训 - 使用第三方代码时,请确保您了解如何正确使用它。这个类绝对没有问题(非常感谢提供者和所有为其他人提供此类代码的人)。我想我只是认为当“with”语句结束时它关闭/处置了它对资源的持有。再次感谢。以上是关于访问路径被拒绝 - C# Directory.Move的主要内容,如果未能解决你的问题,请参考以下文章
Directory.Exists - 访问被拒绝 C# 的 UNC 路径
System.UnauthorizedAccessException:对路径的访问被拒绝(UWP C#)