0

I am working on pdf creator in Asp.net with c#. In my code it created a pdf file and after that i use flush, close and dispost the streamwriter and in the nest function i am opening the same file in the next function i am opening this same file to show in browser pdf viewer. my pdf creation method is used threading and pdf reading function is in normal mode function.

My question is that how to check that this pdf file is use in another process in c# code? Because if i have created very small pdf file it will works fine but if i am creating a pdf file of 5-10 pages in that case it will throws error. i.e. the process cannot access the file because it is being used by another process

I want to use check the process monitor code on that file and how to explicitly kill that process?

Arion
  • 31,011
  • 10
  • 70
  • 88
Gaurav Agrawal
  • 4,355
  • 10
  • 42
  • 61

2 Answers2

0

If it doesn't make sense to open the Stream as FileShare.Read in the first place (because your PDF is being created and it's not yet viewable), then you may as well just try ... catch opening the stream.

At least that's how this popular thread seems to do it.

Community
  • 1
  • 1
Jason
  • 6,878
  • 5
  • 41
  • 55
  • See http://stackoverflow.com/questions/317071/how-do-i-find-out-which-process-is-locking-a-file-using-net and http://stackoverflow.com/questions/177146/how-do-i-get-the-list-of-open-file-handles-by-process-in-c – Jason Mar 16 '12 at 09:39
0

If you are starting process that creates PDF, just wait for it and check exitcode, of course if the process that creates PDF return proper exit codes for success or some error, here is some example code :

Process proc = Process.Start("createpdf.exe", "params");
proc.WaitForExit();
int result = proc.ExitCode;
/// do something based on result
Antonio Bakula
  • 20,445
  • 6
  • 75
  • 102
  • please describe situation with more details, becouse if that process that you are going to kill produces PDF that obviously won't help you – Antonio Bakula Mar 16 '12 at 09:43