C# System.UnauthorizedAccessException对路径的访问被拒绝

C# System.UnauthorizedAccessException对路径的访问被拒绝,c#,exception,.net,pdf-writer,C#,Exception,.net,Pdf Writer,尝试使用以下代码编写PDF文档时: document = new Document(); PdfWriter writer = null; ; try { writer = PdfWriter.GetInstance(document, new FileStream(@"E:\mergFiles", FileMode.Create)); } catch (Exception xc) { } 我得到一个例外: {System.UnauthorizedAccessException: Ac

尝试使用以下代码编写PDF文档时:

document = new Document();
PdfWriter writer = null; ;
try
{
    writer = PdfWriter.GetInstance(document, new FileStream(@"E:\mergFiles", FileMode.Create));
}
catch (Exception xc)
{ }
我得到一个例外:

{System.UnauthorizedAccessException: Access to the path 'E:\mergFiles' is denied.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
   at System.IO.FileStream..ctor(String path, FileMode mode)
   at PDFLibrary.PDFManager.MergeDocs()}
我有权访问此文件夹


Goggle发现这有助于
File.SetAttributes(@“E:\mergFiles”,FileAttributes.Normal)
但我还是遇到了同样的异常。

好吧,你不能将文件夹的名称传递给
文件流。它需要是一个包含路径的文件名(edit:当然,如果使用相对文件名,它实际上不必包含路径)

如果要创建文件夹,请使用
Directory.CreateDirectory
。要在该文件夹中创建文件,请使用以下方法:

string fullName = Path.Combine(pathName, fileName);
writer = PdfWriter.GetInstance(document, new FileStream(fullName, FileMode.Create));

mergFiles
听起来像一个目录,而不是一个文件。所以我不希望它被传递给
FileStream
构造函数。您是否绝对确定“E:\mergFiles”是文件名(如代码所述),而不是像你文章中所说的文件夹?什么样的应用程序?它是文件夹的名称。那么你想要什么呢?你不能让文件名和文件夹名完全相同。做出决定并调整代码以避免冲突。考虑使用<代码>路径> /Cuff>类创建辅助程序来创建路径…