Windows – Different symlink for different user on Windows

symbolic-linkwindows

I have an app which saves its data on C:\ProgramData. That means for all users, it uses the same data. I want to achieve a way, so that different users can have their own separate data space. I have used symlink (folder junction) to create it to %userprofile%\Documents\AppFolder, and the method works fine on my user profile, but when tried with different user the app doesn't run; as it tries to access C:\User\User1\Documents\AppFolder, which User2 doesn't have access. Any possible workaround, so the different users can link to different symlinks?

Best Answer

You can use the subst command to map a drive letter to your user directory. Drive letters created by subst are per-user, so you can create the same mapping as each user and then create a symlink to that drive letter. Or just point the program to the mapped drive via its settings.

An example:

subst v: %userprofile%
mklink /d c:\ProgramData\AppFolder v:\Documents\AppForder

A downside of this solution is that mapped drive letters are not persistent, so you have to recreate them every time a user logs in (for example, by putting a shortcut to the subst command into %appdata%\Microsoft\Windows\Start Menu\Programs\Startup. Or you can make a batch file which will run subst and then your program.

An alternative solution is to run your program in a sandbox (e.g. Sandboxie).This won't work if the program tries to do something low-level (e.g. installing drivers or services). Also you'll have to restore files created by the program from sandbox after each run in you need to use them elsewhere.

Related Question