thomasdclark
thomasdclark

Reputation: 474

Creating symbolic links using Visual Basic

What is the easiest way to create symbolic links using Visual Basic? The following code is something simple that I wrote up using the Win32 API, but does not seem to be working:

Imports System.Runtime.InteropServices
Public Class SymbolicLinker
   Private Enum SymbolicLink
      File = 0
      Directory = 1
   End Enum

  <DllImport("kernel32.dll")> _
  Private Shared Function CreateSymbolicLink(lpSymlinkFileName As String, lpTargetFileName As String, dwFlags As SymbolicLink) As Boolean
  End Function
End Class

What is confusing me is that the following C# code works fine:

using System.Runtime.InteropServices;

namespace Testbed_SymbolicLinks
{
   class SymbolicLinker
   {
       [DllImport("kernel32.dll")]
       static extern bool CreateSymbolicLink(string lpSymlinkFileName, string lpTargetFileName, SymbolicLink dwFlags);

       enum SymbolicLink
       {
           File = 0,
           Directory = 1
       }
}

}

Am I somehow porting the code incorrectly from C# to VB?

EDIT: My use case is very simple:

Module Module1
    Sub Main()
        Console.WriteLine("Entering application.")
        Try
            CreateNewSymbolicLink()
        Catch e As Exception
            Console.WriteLine("An error occured.")
        End Try
        Console.ReadKey()
    End Sub
    Sub CreateNewSymbolicLink()
        Console.WriteLine("Enter the name of a valid file:  ")
        Dim file As String = Console.ReadLine()
        Console.WriteLine("Enter the name of a symbolic file:  ")
        Dim symbol As String = Console.ReadLine()
        Dim success As Boolean = SymbolicLinker.CreateSymbolicLink(symbol, file, SymbolicLink.File)
        If success Then
            Console.WriteLine("Web call was success")
        Else
            Console.WriteLine("Web call was a failure")
        End If
    End Sub
End Module

Upvotes: 0

Views: 3277

Answers (1)

David Heffernan
David Heffernan

Reputation: 612954

You have not shown your real code, so it's impossible to say what went wrong. Here is code that does work:

Imports System.Runtime.InteropServices

Public Class SymbolicLinker

    Public Enum SymbolicLink
        File = 0
        Directory = 1
    End Enum

    <DllImport("kernel32.dll", SetLastError:=True)> _
    Public Shared Function CreateSymbolicLink(lpSymlinkFileName As String, _
                                              lpTargetFileName As String, _
                                              dwFlags As SymbolicLink) As Boolean
    End Function

End Class

Module Module1

    Sub Main()
        CreateNewSymbolicLink("C:\Users\xxx\Desktop\link.txt", _
                              "C:\Users\xxx\Desktop\foo.txt")
        Console.ReadKey()
    End Sub

    Sub CreateNewSymbolicLink(linkName As String, targetName As String)
        If Not SymbolicLinker.CreateSymbolicLink(linkName, targetName, _
                                                 SymbolicLinker.SymbolicLink.File) Then
            Console.WriteLine(Marshal.GetLastWin32Error())
        End If
    End Sub

End Module

Note that I added the SetLastError parameter to the DllImport attribute. This allows us to capture the error code in case of failure. I also demonstrated how to obtain that error code, and also when to do so. That is, if and only if, the function return value indicates failure.

Upvotes: 5

Related Questions