Getting the location of the program (gets the location of itself)

I'm trying to get a program to find its own location.

Page 1 of 1

3 Replies - 763 Views - Last Post: 24 April 2008 - 09:45 AM Rate Topic: -----

#1 devilsson2010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 02-January 08

Getting the location of the program (gets the location of itself)

Posted 23 April 2008 - 01:29 PM

I was trying to make a program that would delete itself, and I wanted it to be able to find its own location. So wherever I run it from, it can get that location then delete itself, oh and it should get what it's named too.

Oh and I also does anyone know the file path to the recycle bin so I can have the program delete itself from that.

I couldn't find anything on finding its own location and name, so any help is welcome, thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Getting the location of the program (gets the location of itself)

#2 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Getting the location of the program (gets the location of itself)

Posted 23 April 2008 - 01:55 PM

why would you want to do that?

Also, you would have to delete the file before emptying the recycle bin. And because the program is deleted, it won't be able to execute any following code that might follow (ie. emptying the recycle bin).

Before you try this, I'm not holding myself responsible for any files that you might delete.

To delete a file, you could use (on Windows) system ("del some_program.exe");
But that would only delete a specific file.
NOTE: It will start in the directory of the current application by default, so if your program is in C:\folder\another folder\ it will delete from this path.
Was This Post Helpful? 0
  • +
  • -

#3 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: Getting the location of the program (gets the location of itself)

Posted 23 April 2008 - 03:09 PM

GetModuleFileName will get you the name of the file under Windows (include <windows.h>). GetModuleFileName can be called with NULL as it's first argument to retrieve the current module's file name. DeleteFile will delete the file. Note: you may have difficulty deleting a file that is being executed (as it's in use when your executing it).

See MSDN for more information on these functions.
Was This Post Helpful? 0
  • +
  • -

#4 devilsson2010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 02-January 08

Re: Getting the location of the program (gets the location of itself)

Posted 24 April 2008 - 09:45 AM

ha, yea deleting itself then deleting itself from the recycle bin could be a problem. however, i know you can delete itself while it's running because i've done it before, it might be because it's not actually deleted (its held in the recycle bin), or there is some kind of temporary execution thing that windows uses. Either way thanks for the GetModuleFileName.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1