简体   繁体   English

System.UnauthorizedAccessException拒绝访问路径

[英]System.UnauthorizedAccessException Access to the path is denied

At attempt to write PDF document with following code: 尝试使用以下代码编写PDF文档:

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

I am getting an exception: 我得到一个例外:

{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()}

I have all access to this folder. 我有权访问该文件夹。

goggled and found that this could help File.SetAttributes(@"E:\\mergFiles", FileAttributes.Normal); goggled并发现这可以帮助File.SetAttributes(@"E:\\mergFiles", FileAttributes.Normal); but still I'm getting the same exception. 但我仍然得到同样的例外。

Well, you can't pass the name of a folder to a FileStream . 好吧,您不能将文件夹的名称传递给FileStream It needs to be a file name including the path ( edit : of course it doesn't really have to include the path if you use a relative file name). 它必须是包含路径的文件名( 编辑 :当然,如果使用相对文件名,它实际上不必包含路径)。

If you want to create a folder, use Directory.CreateDirectory . 如果要创建文件夹,请使用Directory.CreateDirectory To create a file within that folder, use something like this: 要在该文件夹中创建文件,请使用以下命令:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.UnauthorizedAccessException:访问路径:拒绝PATH - System.UnauthorizedAccessException: Access to the path:PATH is denied DotNetZip System.UnauthorizedAccessException:拒绝访问路径 - DotNetZip System.UnauthorizedAccessException: Access to the path is denied System.UnauthorizedAccessException:拒绝访问路径“...” - System.UnauthorizedAccessException: Access to the path '...' is denied UWP System.UnauthorizedAccessException:拒绝访问路径 - UWP System.UnauthorizedAccessException: Access to the path is denied System.UnauthorizedAccessException:拒绝访问路径“...” - System.UnauthorizedAccessException: Access to the path "..." is denied System.UnauthorizedAccessException:访问路径被拒绝 - System.UnauthorizedAccessException: Access to the path denied System.UnauthorizedAccessException:访问路径拒绝错误 - System.UnauthorizedAccessException: Access to the path denied error FileStream打开图像“ System.UnauthorizedAccessException”,拒绝访问该路径 - FileStream to open an image “System.UnauthorizedAccessException” Access to the path is denied 如何解决system.unauthorizedaccessexception对路径的访问被拒绝 - How to solve system.unauthorizedaccessexception access to the path is denied 异常:System.UnauthorizedAccessException,消息:拒绝访问路径“” - Exception: System.UnauthorizedAccessException, Message: Access to the path “” is denied
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM