4 Replies - 566 Views - Last Post: 13 August 2013 - 11:24 AM Rate Topic: -----

#1 timlacee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-November 12

Process.Close to close dialog window

Posted 13 August 2013 - 10:19 AM

Hello, I'm trying to figure out how to close a Process I've started in C# but keep getting an exception and can't figure out what I'm doing wrong. Below is what I have so far. It should just simply close the window it has opened but that doesn't happen. I thought using this method would be easier to use to open a zip folder rather than anything else I've researched and it opens just fine using windows explorer. It just won't close...

private void button1_Click(object sender, EventArgs e)
        {
                
                    //create variable to store zip file location
                    Process unZipApp; //var for 1st zip folder

                    //start the windows explorer app to unzip ejarchive.zip and EJArchives.zip
                    unZipApp = Process.Start("C:\\Users\\timla_000.TIM-PC\\Desktop\\ejarchive.zip\\EJArchives.zip");

                    Thread.Sleep(2000); //Wait 2 seconds before closing the windows                 

                    unZipApp.Close();                                            
        }


Any ideas? I'm very new to programming and have been studying C# on my own.

Is This A Good Question/Topic? 0
  • +

Replies To: Process.Close to close dialog window

#2 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2533
  • View blog
  • Posts: 4,633
  • Joined: 24-September 10

Re: Process.Close to close dialog window

Posted 13 August 2013 - 10:44 AM

What is the exception that is thrown?



Also, what is Process, and what does Close do? Is it this Process class from the .Net framework?

http://msdn.microsof...cess.close.aspx

Cause it doesn't say that Close closes the window...



Lastly, a 'Process' is hardly a dialog window. Your title is misleading.
Was This Post Helpful? 0
  • +
  • -

#3 timlacee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-November 12

Re: Process.Close to close dialog window

Posted 13 August 2013 - 11:10 AM

I apologize. I didn't intend to be misleading. I need to work on my terminology among other things. The exception is "object reference not set to an instance of an object". I viewed the link you attached and it was enlightening. I have a feeling what I'm trying to do isn't going to work. It works perfectly now if I use it with notepad but it doesn't like trying to close
unZipApp = Process.Start("C:\\Users\\timla_000.TIM-PC\\Desktop\\ejarchive.zip\\EJArchives.zip"); 
I'll probably have to look at another method to open .zip files.
Was This Post Helpful? 0
  • +
  • -

#4 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2533
  • View blog
  • Posts: 4,633
  • Joined: 24-September 10

Re: Process.Close to close dialog window

Posted 13 August 2013 - 11:18 AM

This is the method you're calling for Start:

http://msdn.microsof...y/53ezey2s.aspx

Note what it says about the return value:

Quote

Return Value
Type: System.Diagnostics.Process
A new Process component that is associated with the process resource, or null, if no process resource is started (for example, if an existing process is reused).


You're getting an access of a null object reference error when you call 'Close', well the only object that could be null is the process that you're attempting to close.

Your process is retrieved by that 'Process.Start' method, which has the ability to return null under certain circumstances. You should be testing for if it's null or not.
Was This Post Helpful? 1
  • +
  • -

#5 timlacee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-November 12

Re: Process.Close to close dialog window

Posted 13 August 2013 - 11:24 AM

Thats it! Thank you very much sir!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1