4

I've been working on developing a program to manage symbolic links to folders, which has worked great, until I moved to continuing the work on a Windows 8 machine (from Windows 7). Since doing that, my CreateSymbolicLink() method has returned an error code of 2.

This is the method I have that is called whenever I want to link a directory. Before this is called, the original folder has been moved to what is destDirName

public static void LinkDirectory(string sourceDirName, string destDirName)
{
    if (!CreateSymbolicLink(sourceDirName, destDirName, 0x1))
    {
        MessageBox.Show("Error: Unable to create symbolic link. " + 
            "(Error Code: " + Marshal.GetLastWin32Error() + ")"); 
    }
}

This is the imported method from kernel32.dll:

[DllImport("kernel32.dll")]
static extern bool CreateSymbolicLink(string lpSymlinkFileName, 
    string lpTargetFileName, int dwFlags); 

And the logic:

FileFunctions.MoveDirectory(gameOriginalSaveFolder, gameGatheredSaveFolder);
FileFunctions.LinkDirectory(gameOriginalSaveFolder, gameGatheredSaveFolder);
FileFunctions.HideDirectory(gameOriginalSaveFolder);

Using a breakpoint, this is the locals that are sent with the above logic: https://i.stack.imgur.com/zl2Ns.png

To note, this code worked fine when I was developing it under Windows 7, but has suddenly stopped working since. I hope this is enough info to clear some stuff up, otherwise, please ask.

mtaanquist
  • 506
  • 4
  • 16

1 Answers1

7

After playing around with this further, I managed to find out that this is a permissions issue, as you cannot turn User Account Control off entirely in Windows 8. As such, the development environment that Visual Studio uses was not elevated and could not run the program.

I managed to solve the issue by following the steps outlined here: https://stackoverflow.com/a/12859334/1862405

Community
  • 1
  • 1
mtaanquist
  • 506
  • 4
  • 16