【问题标题】:System.UnauthorizedAccessException Access to the path is deniedSystem.UnauthorizedAccessException 对路径的访问被拒绝
【发布时间】:2013-08-22 07:28:26
【问题描述】:

尝试使用以下代码编写 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);,但我仍然遇到同样的异常。

【问题讨论】:

  • mergFiles 听起来像一个目录,而不是单个文件。所以我不希望它被传递给FileStream 构造函数。
  • 您确定“E:\mergFiles”是文件名(如代码所示),而不是您帖子中文本所述的文件夹吗?
  • 什么样的应用程序?
  • 文件夹名
  • 那么你想要什么 - 你不能让文件名和文件夹名相同 - 做出决定并调整代码以避免冲突。考虑使用来自Path 类的助手来创建路径...

标签: c# exception .net pdf-writer


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2017-01-24
    • 1970-01-01
    • 2021-04-02
    • 1970-01-01
    • 2018-04-05
    • 1970-01-01
    • 2021-06-19
    相关资源
    最近更新 更多