Search code examples
pythonwindowsshortcut-file

How to pass current directory in Windows shortcut target?


So I have a Python program written, and what I am trying to do is to create windows shortcut to the program, and making sure that when running the shortcut, not only the software is ran but also the current directory that the shortcut is at would be passed as sys.argv, so that I could do something to it in the program. I know that for windows shortcut, you can do something like this:

"C:\Documents and Settings\Administrator\Desktop\hello.py" --somevalue

to pass in arguments to the target. I am wondering how could I make it work for my specific case? So for example, if we put the shortcut at Desktop, and run the shortcut, the Python software would start with sys.argv[1]="C:\Users\username\Desktop".


Solution

  • You can specify the current directory for the executed application in a .LNK, that is the normal way to do things if you depend on a specific directory.

    If you for some reason actually need to get information about the .LNK you must call the GetStartupInfo Windows function (with ctypes I assume) and if the STARTF_TITLEISLINKNAME bit is set in STARTUPINFO.dwFlags then STARTUPINFO.lpTitle contains the path to the .LNK the user used to start the application.