[英]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.