11 Replies - 928 Views - Last Post: 07 February 2011 - 09:04 PM Rate Topic: -----

#1 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

How can I accomplish this file deletion elegantly?

Posted 07 February 2011 - 07:09 PM

Basically the user clicks a button, I generate a PDF and open it. Is there a way to delete this generated file once he closes it? It's not really important but if it's possible I'll include it. :)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using Foo.Data.Repositories;
using Foo.Branding;
using System.IO;
using System.Diagnostics;

namespace Tutomentor.Reporting
{
    public class StudentList
    {
        public void PrintStudentList(int gradeParaleloID)
        {
            StudentRepository repo = new StudentRepository();
            var students = repo.FindAllStudents()
                                .Where(s => s.GradeParaleloId == gradeParaleloID);

            string filePath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\Alumnos.pdf";

            try
            {
                Document document = new Document(PageSize.LETTER);
                BaseFont times = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, BaseFont.EMBEDDED);
                Font font = new Font(times, 12);
                
                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(filePath, FileMode.Create));
                document.Open();
                //MAGIC GOES HERE.
                document.Close();

                Process.Start(filePath);
            }
            catch (DocumentException de)
            {
                Debug.WriteLine(de.Message);
            }
            catch (IOException ioe)
            {
                Debug.WriteLine(ioe.Message);
            }

        }
    }
}


Thank you for the help.

This post has been edited by Sergio Tapia: 07 February 2011 - 07:10 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How can I accomplish this file deletion elegantly?

#2 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4440
  • View blog
  • Posts: 7,721
  • Joined: 08-June 10

Re: How can I accomplish this file deletion elegantly?

Posted 07 February 2011 - 07:54 PM

I'd say instead of using the static Process.Start, make an instance and use a ProcessStartInfo to start the process. Subscribe to the Exited event of the process. Once the process exits, delete the file.
Was This Post Helpful? 0
  • +
  • -

#3 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: How can I accomplish this file deletion elegantly?

Posted 07 February 2011 - 08:10 PM

Can't find that event, any suggestions?

ProcessStartInfo info = new ProcessStartInfo(filePath);
info.Exited?


Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5442
  • View blog
  • Posts: 11,676
  • Joined: 02-June 10

Re: How can I accomplish this file deletion elegantly?

Posted 07 February 2011 - 08:19 PM

Do you really have to use an external program to view the PDF? The WebBrowser component can show it just like I.E. does.
If you make your own form with a webBrowser on it to show the PDF you then maintain complete control and have your own buttons like "Done" so you have a trigger for closing.
Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4440
  • View blog
  • Posts: 7,721
  • Joined: 08-June 10

Re: How can I accomplish this file deletion elegantly?

Posted 07 February 2011 - 08:20 PM

The event is on the Process class, not the ProcessStartInfo. You use the PSI as a parameter for the Process class.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5442
  • View blog
  • Posts: 11,676
  • Joined: 02-June 10

Re: How can I accomplish this file deletion elegantly?

Posted 07 February 2011 - 08:22 PM

--
THought...
Can you create to a memorystream instead of a filestream (faster) then pass the filestream to your process? When the process closes you should get an event something like .Disposed to let you know the user is done with it.
Was This Post Helpful? 0
  • +
  • -

#7 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: How can I accomplish this file deletion elegantly?

Posted 07 February 2011 - 08:25 PM

These PDFs are actually reports meant to be printed. I decided to just let the default PDF software on the target machine handle it. Unless there is a way to print a PDF from the WebBrowser control?
Was This Post Helpful? 0
  • +
  • -

#8 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: How can I accomplish this file deletion elegantly?

Posted 07 February 2011 - 08:31 PM

Thanks for the help guys, I came up with the following:

ProcessStartInfo info = new ProcessStartInfo(filePath);
Process process = new Process();
process.StartInfo = info;
process.Exited += new EventHandler(x_Exited);

process.Start();

void x_Exited(object sender, EventArgs e)
{
    Debug.WriteLine("It exited");
}



I put a breakpoint on the event, but even after closing Adobe PDF reader it doesn't fire. Something must be wrong no?

Any ideas?
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5442
  • View blog
  • Posts: 11,676
  • Joined: 02-June 10

Re: How can I accomplish this file deletion elegantly?

Posted 07 February 2011 - 08:39 PM

Did you close the document or the application? If the application is still running then I bet the process has not exited.

Also, you have a memory leak issue here. You have subscribed to an event (+=) with no corresponded unsubscribe (-=).
Was This Post Helpful? 1
  • +
  • -

#10 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: How can I accomplish this file deletion elegantly?

Posted 07 February 2011 - 08:43 PM

I thought the GC handled that for me? Can anyone else confirm this?

Also when I closed the PDF I close the Adobe PDF Reader (what the secretary will do). Is that what you meant by "Did you close the document or the application? If the application is still running then I bet the process has not exited."?
Was This Post Helpful? 0
  • +
  • -

#11 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4440
  • View blog
  • Posts: 7,721
  • Joined: 08-June 10

Re: How can I accomplish this file deletion elegantly?

Posted 07 February 2011 - 08:59 PM

As a matter of fact, that's one thing the GC doesn't handle. It can't, because that subscription is keeping a reference to the object alive. GC can't collect until all references are destroyed.

Another thing you can try is start a loop, and check the HasExited property of the process, there might be an issue with the event. I'm not sure.

tlhIn'toq's suggestion is also correct. Sometimes, a window can close, but the application can stay open.

I like his idea with the WebBrowser control. Adobe does some weird stuff...who knows what's going on with that process.
Was This Post Helpful? 0
  • +
  • -

#12 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5442
  • View blog
  • Posts: 11,676
  • Joined: 02-June 10

Re: How can I accomplish this file deletion elegantly?

Posted 07 February 2011 - 09:04 PM

We just did this same thing about the subscriptions on another thread just a few days ago.
That same problem cost these DARPA competitors about $2 million.
http://www.codeproje...TSProfiler.aspx
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1