Forum How do I...?

Access is Denied from .NET Console App

khurwitz
After more research, I was able to reproduce the "Access is Denied -- Please check system permission to run Prince." error from a very simple console app written in C#.NET. The full source code is below:

internal class Program
{
private static void Main(string[] args)
{
var prince = new Prince(@"C:\prince");
prince.Convert(@"c:\prince\sample.html", @"c:\prince\sample.pdf");
}
}

Note, my operating system is a clean install of Windows 7 (64 bit). To keep things simple, I have placed prince.exe (the command line version) in "C:\prince".
mikeday
Can you successfully run Prince from the command-line by hand?

When you run the C# program, is it throwing any particular exception? I can't find any references to the error message that you quoted.
khurwitz
Thank you so much for your reply. I realized my mistake this morning - it was a pretty embarrassing one. When initializing the Prince object, you need to pass in the FULL prince executable filename, not just the path to it. I was thinking the API would append the prince.exe on the end for me. The error I was receiving is one generated specifically by the .NET API, which is probably why you didn't recognize it.

The only suggestion for improvement on this would be to have the .NET API try to verify the prince command-line executable file existed on the hard drive (in the location specified) before calling Process.Start(). If you did this, you could throw a special "Prince executable file not found" exception, instead of the "Access is denied" error returned by the .NET framework. The "Access is denied" error made me believe it had found the executable, but was not allowing me to execute it for some security reason (which is a common problem with all of the .NET Code Access Security hullabaloo - particularly with web apps). I hope my post and your reply will help others in the future.

Overall, I can't tell you how impressed I am with the feature set, standards-based approach, and quality of the PrinceXML product - It's truly a game-changing technology.
mikeday
Sorry, I didn't even notice the executable path; normally I would check that first. :)

It would make sense to check for an executable file before invoking it, in order to give a better error message. I'll see if we can do this for a future release.
khurwitz
The .NET Framework makes this task very easy. You can use the following code:

if (!File.Exists(princeExeArgument))
{
throw new FileNotFoundException("Prince Executable Not Found in the location specified. Check the Prince constructor parameter...")
}

Of course, princeExeArgument would be the parameter you received in the constructor of the Prince object.