2

I use below line of code to copy an open exe into another folder (windows startup)

File.Copy(ownPath, Path.Combine(startupFolder, "Player.exe"));

but the problem is my exe size is 16 MB and its copied 12KB. So how can I full copy an exe if it is open. I check here on stack it said that if the exe is open then it cannot be copied fully. So what to do it is mandatory for me to add exe into startup folder when the app run first time.

Community
  • 1
  • 1
Muhammad Faizan Khan
  • 10,013
  • 18
  • 97
  • 186

1 Answers1

2

An answer here suggested using Shadow Copy: How to copy a file while it is being used by other process

But I am not sure if a Shadow Copy is always available.

Are you trying to get the program to launch on Windows Startup? If so you can do so without making a copy: How to make an exe start at the Windows Startup

Or if you really need to make a copy, then you can make sure the program is not already running, and if it is, ask the user to stop it first: Check if a specific exe file is running

Edit:

I have found no mention of working directory in the registry's documentation in MSDN. There may be two workarounds: 1. If the executable supports it, you can pass the working directory as a parameter. 2. You can try using a launcher in the registry, e.g. cmd.exe or powershell:

cmd.exe /c [Batch file to change directory and launch EXE]

powershell.exe -windowstyle hidden start-process [Path to EXE] -WorkingDirectory [WorkingDir] The -windowstyle hidden only works for Powershell V2. You'll otherwise get a console window briefly.

Community
  • 1
  • 1
KC Wong
  • 2,410
  • 1
  • 18
  • 26
  • Yes actully I want to add my Unity3d Application into windows startup so that it run automatically. But the problem is its exe only runs if the it data folder present next to it. So I have to add both exe and its data folder in startup of windows. – Muhammad Faizan Khan Oct 20 '16 at 04:58
  • Is there anyway available to make my exe shortcut programatically and add it to startup so that it runs correctly. but i must have to open my exe for making its shortcut – Muhammad Faizan Khan Oct 20 '16 at 05:00
  • I am note getting any exeception when copying an open exe into startup. – Muhammad Faizan Khan Oct 20 '16 at 05:01
  • In the link @KCWong provided there is an answer describing how to add a shortcut to Windows Startup by using the registry. This might solve your data folder needs as no files will be actually voved – Tadija Bagarić Oct 20 '16 at 05:03
  • But still, this would just be a work around and not solving the problem described in this question. To be honest with you I am kinda suprised that copying a file that is getting executed isn't working. – Jannik Oct 20 '16 at 05:16
  • Files can be read-locked. Windows read-lock executables when running them. http://stackoverflow.com/questions/196897/locking-executing-files-windows-does-linux-doesnt-why – KC Wong Oct 20 '16 at 05:23