2

I am receiving the following error sporadically and rarely:

Access to the path 'e:\Batch\CR\data_Test\IM_0232\rdlcTemp\RN837102.ZM\MemberEOB_1.pdf' is denied.

The exact block of code where the error occurs is shown below. This error only occurs sporadically and rarely. If we reboot the server and try to run the same input file again, we will find that the error does not occur again - but not always. This code is in a loop which can be executed up to 5000 times in a single run of the application. We find the error can occur at any point in the run - 100s of PDF files will be created without any problem and then this error will occur. Why are we getting this error?

Relevant code:

byte[] bytes = report.Render("PDF", deviceInfo);
FileStream fs = new FileStream(@savePath + ".pdf", FileMode.Create);
fs.Write(bytes, 0, bytes.Length);
fs.Close();
fs.Dispose();
JoshDM
  • 4,939
  • 7
  • 43
  • 72
user2371688
  • 31
  • 2
  • 4
  • 2
    1) `using(FileStream fs = new FileStream(...)) {` 2) `synchronized(this) {` or similar to avoid problems if there are more than one thread. 3) could it be something an outside app is doing? Possibly also run Handle: http://stackoverflow.com/questions/317071/how-do-i-find-out-which-process-is-locking-a-file-using-net –  Jul 08 '13 at 20:59
  • Are all the PDF files being created in that same folder as above? Also, is UAC on the server OS you are using? – user959631 Jul 08 '13 at 21:11
  • Maybe there is some other process which blocks the file? Like an indexer or a virus scanner? – Christian Sauer Jul 17 '13 at 14:23

3 Answers3

1

Is it posible that the input/output process is so intense that the device where the PDF file it's being created is busy ? it does not hurt to check the EVENT VIEWER to see if anything related is reported there

Mauricio Gracia Gutierrez
  • 10,288
  • 6
  • 68
  • 99
0
  1. Navigate to rdlcTemp from path e:\Batch\CR\data_Test\IM_0232\rdlcTemp\RN837102.ZM\
  2. Right click over RN837102.ZM folder
  3. Click Properties->Security
  4. Make sure that Write permission is checked for this file for current account user
  5. If it's not checked click Edit and Enable Write Permission
  6. Apply changes

Hope this help

Nick
  • 4,192
  • 1
  • 19
  • 30
0

Since this isn't solved yet, here is the relevant code from my suggestions:

static object staticSyncObject = new object();  // in class level fields
// ... 
foreach( string savePath in paths )
{
    byte[] bytes = report.Render("PDF", deviceInfo);
    lock(staticSyncObject) // synchronized() was java, ideally would time out
    {
        using(FileStream fs = new FileStream(@savePath+".pdf", FileMode.Create))
        {
            fs.Write(bytes, 0, bytes.Length);
        }
    }
}

In exception handling code:

} catch (Exception ex) {
    Console.WriteLine("" + ex);

    System.Diagnostics.Process p = new System.Diagnostics.Process();
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.FileName = "handle.exe";
    p.StartInfo.Arguments = "C:\\path_to_pdfs";
    p.Start();

    int ch;
    while ((ch = p.StandardOutput.Read()) != -1)
        Console.Write((char)ch);
}
  • It is this latest code copied above that we implemented. We have checked the Event Viewer and found nothing. What is UAC? Thank you. – user2371688 Jul 17 '13 at 19:58
  • @user2371688 UAC stands for User Account Control. It's the little warning that pops up every time any administrative action is taken on say Windows 7. What were you hoping for in the event viewer? This code writes to console `stdout`. You could modify to write somewhere else if it'll help. (Debug file, system debugger or even event viewer I suppose) Personally `System.Diagnostics.Trace.WriteLine("foo");` with DbgView is my favorite. (DbgView: http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx) –  Jul 17 '13 at 20:04
  • Is the suggestion about implementing System.Diagnostics.Trace.WriteLine("foo"); intended to be a fix of the "access denied" error? I am not understanding how it could be a fix – user2371688 Jul 22 '13 at 13:38
  • @user2371688 It was meant to be a fix for: "We have checked the Event Viewer and found nothing." (My example doesn't write to the event viewer... `Trace.WriteLine()` could replace `Console.Write()`.) –  Jul 22 '13 at 13:51