系统未经授权的访问异常:拒绝访问路径。

6
尝试使用以下代码编写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()}

我对这个文件夹拥有完全访问权限。

我搜索了一下并发现这个命令可能有用:File.SetAttributes(@"E:\mergFiles", FileAttributes.Normal); 但是依旧出现相同的异常。


4
"mergFiles"听起来像是一个目录,而不是单个文件。因此我不会期望它被传递给FileStream构造函数。 - Damien_The_Unbeliever
你确定 "E:\mergFiles" 是文件名(如代码所示),而不是你帖子中所说的文件夹吗? - Alexei Levenkov
这是一个文件夹的名称 - yogesh lawate
那么你想要什么 - 你不能让文件名和文件夹名相同 - 做出决定并调整代码以避免冲突。考虑使用Path类中的帮助程序创建路径... - Alexei Levenkov
1个回答

11

您不能将文件夹名称传递给FileStream。 它需要是包括路径的文件名(编辑: 如果使用相对文件名,则实际上不需要包括路径)。

如果要创建一个文件夹,请使用Directory.CreateDirectory。 要在该文件夹中创建文件,请使用以下类似代码:

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

我看到这个错误 'Object reference not set to an instance of an object.'。 - MindRoasterMir

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接