Runtime Security Exception: System.Security.Permissions.FileIOPermission

Language: VB.NET

Runtime Security Exception occurs for the following code:

Dim doc As Aspose.Words.Document
doc = New Document(mydir + "mydoc.docx") <<<<<<<<

Runtime Security Exception:
Request for the permission of type ‘System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’ failed.

How do I remedy the situation?

Thanks -

Hi

Thanks for your inquiry. Most likely, you have no permissions to read this document on your environment. Please make sure you have needed rights.

Also could you please attach your input document here for testing? I will check it on my side and provide you more information.

Best regards,

My local environment has permissions to the document, I can view it, and my VB.net program can access the document using other libraries.

Example of it working:

Imports System.Runtime.InteropServices
Imports Microsoft.Office.Interop.Word
docWord = appWord.Documents.Open("myDoc.docx")

I did notice that the the Aspose.Words.dll I am referencing in my project is on a mapped drive to a different server.

Would the insufficient permissions of that .dll on a remote server produce such an exception?

How would I fix this (please be technical and specific if you can)?

Thanks,

Hi

Thank you for additional information. As an option, you can read your document into stream and then open document from stream. You can use code like the following:

// Open document into stream.
Stream docStream = File.OpenRead(@"C:\temp\in.doc");
// Create Document object from stream.
Document doc = new Document(docStream);

If this will not help, try specifying the following options upon reading the document into stream.

// The specified file can be opened in MS Word in moment of opening it using Aspose.Words.
string fileName = @"C:\Temp\in.doc";
// Open docuemnt 
FileStream docStream = new System.IO.FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
// Create Aspose.Words Document from stream
Document doc = new Document(docStream);
// Close stream
docStream.Close();
// Do somethisng with document
// Save document
doc.Save(@"C:\Temp\out.doc");

Hope this helps.

Best regards,

Hi Alexy (or anyone else on Aspose tech staff who may know),

I am now receiving the same System.Security.Permissions.FileIOPermission exception on the document.Save() portion of the above code you sent (code as I have it below):

file = "InputTemplate.docx"
documentStream = New System.IO.FileStream(directory & file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
document = New Document(documentStream)
addressees = addresseeCtl.getCorrespondenceAddressees()
document.MailMerge.Execute(addressees)
documentStream.Close()
document.Save("OutputCorrespondenceLetter.docx")

How do I remedy the situation to where I can save the document that’s been mail merged as a new one?

Thanks -

Hi

Thanks for your request. Could you please make sure that you have write permissions to this folder. For instance, try creating a simple text document from code:

using (StreamWriter txtFileWriter = File.CreateText(@"C:\Temp\test.txt"))
{
    txtFileWriter.Write("Hello World!!!");
}

If this code will also throw an exception, then there are some problems with permissions on your side.

Best regards,

Hi Alexy,

This works and does not throw the Security Exception:

txtFileWriter = System.IO.File.CreateText(directory & "Test1.txt")
txtFileWriter.Write("Hello World!!!") 

This does not work and throws the same Security Exception mentioned in the previous posts:

document.Save(directory & "OutputCorrespondenceLetter.docx")

Thanks -

Hi

Thanks for your request. You can try using code like the following to save the document:

using (Stream docStream = File.Create(@"C:\Temp\test.docx"))
{
    doc.Save(docStream, SaveFormat.Docx);
}

Hope this helps.

Best regards,

I get the same exception on the doc save line:

using (Stream docStream = File.Create(@"C:\Temp\test.docx"))
{
    doc.Save(docStream, SaveFormat.Docx); <<<<<<<<<<<<<<<<<<<<<<
}

Hi Steve,

Thanks for your inquiry.

Have you tried moving the Aspose.Words.dll to the same server as the application and seeing if the exception disappears?

Regarding your request for best mail merge practices, you can find full details on mail merge in the documentation here. Which mail merge methods were you using that become “obselete”?

Thanks,

Hello

Thank you for additional information.
Please try the following code: (and please read this article, perhaps it will be useful.)

private static void Main(string[] args)
{
    var license = new License();
    license.SetLicense("Aspose.Words.lic");
    const string path = @"X:\";
    FileIOPermission permission = new FileIOPermission(FileIOPermissionAccess.AllAccess, path);
    permission.AddPathList(FileIOPermissionAccess.AllAccess, path + "input.docx");
    try
    {
        permission.Assert();
        Document doc = new Document(path + "input.docx");
        doc.Save(path + "output.docx", SaveFormat.Docx);
    }
    catch (SecurityException ex)
    {
        Console.WriteLine(ex.Message + ex.StackTrace);
    }
}

And also about the fact you have a library located on another server. Is it possible to move it closer to your application? Or add to the GAC on the machine where you run the project.

Unfortunately this did not work either. I still believe that it is something to do with Code Access Security (CAS), but can’t figure out what exactly to do to modify it. I just gave up and made IIS recognize it as a Classic .NET app pool.

IIS Manager --> Sites --> [WebSite] --> Application Pool --> Classic.NET AppPool

It looks like it is working now. So this isn’t a real solution but a work around.

Hi Steve,

It’s great that it’s working now. Did you ever try moving the DLL to the same location and seeing if that solved the issue? This could provide further insight into where the real issue was coming from.

Thanks,