Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
i need to create a symbolic link in vb.net, i already know i need to elevate permissions which i already completed but i have no clue where to go from there
 
here is what i have so far:
 
Imports System.Text
Imports System.IO
Imports System.Runtime.InteropServices
 
Public Class Main
    <dllimport("kernel32.dll")> _
    Private Shared Function CreateSymbolicLink(ByVal lpSymlinkFileName As String, ByVal lpTargetFileName As String, ByVal dwFlags As Integer) As Boolean
    End Function
 

    Private Sub StartBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartBtn.Click
 

        Try
            CreateSymbolicLink(SteamFrom, SteamTo, 1)
            Application.DoEvents()
        Catch ex As Exception
            MsgBox(ex.ToString)
            Return
        End Try
 
        MsgBox("Done")
    End Sub
 

End Class
Posted 20-Jan-13 22:22pm
ldrrp1108
Edited 20-Jan-13 22:49pm
v3
Comments
Richard MacCutchan at 21-Jan-13 4:49am
   
And what happens?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Here it is a working sample:
 
Imports System.Text
Imports System.IO
Imports System.Runtime.InteropServices
 
Module Module1
    <dllimport("kernel32.dll",> _
    Private Function CreateSymbolicLink(ByVal lpSymlinkFileName As String, ByVal lpTargetFileName As String, ByVal dwFlags As Integer) As Integer
    End Function
 
    <dllimport("kernel32.dll",>
    Private Function FormatMessage(
        ByVal dwFlags As Integer,
        ByVal lpSource As String,
        ByVal dwMessageId As Integer,
        ByVal dwLanguageId As Integer,
        ByVal lpBufferas As StringBuilder,
        ByVal nSize As Integer,
        ByVal Arguments() As String) As Integer
    End Function
 
    Private Function GetLastDllErrorMessage(ByVal errNumber As Integer) As String
        Dim strLastErrorMessage As New StringBuilder(255)
        FormatMessage(4096, Nothing, errNumber, 0, strLastErrorMessage, strLastErrorMessage.Capacity, Nothing)
        Return strLastErrorMessage.ToString
    End Function
 
    Sub Main()
        Dim SteamFrom = "<path to="" the="" new="" link="" be="" created="">"
        Dim SteamTo = "<path to="" an="" existing="" file="">"
        Const SYMLINK_FLAG_DIRECTORY As Integer = 1
        Const SYMLINK_FLAG_FILE As Integer = 0
 
        Try
            If CreateSymbolicLink(SteamFrom, SteamTo, SYMLINK_FLAG_FILE) = 1 Then
                MsgBox("Done", MsgBoxStyle.Information)
            Else
                MsgBox(GetLastDllErrorMessage(Err.LastDllError), MsgBoxStyle.Critical)
            End If
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical)
        End Try
    End Sub
End Module
</path></path>
 
Remember that both files need to be in the same drive and OS must be NT.
If you want to link to a folder instead a file, use the constant SYMLINK_FLAG_DIRECTORY instead of SYMLINK_FLAG_FILE.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Gihan Liyanage 392
1 ClimerChinna 247
2 Sergey Alexandrovich Kryukov 225
3 vikinghunter 176
4 OriginalGriff 161
0 Sergey Alexandrovich Kryukov 8,373
1 OriginalGriff 7,112
2 CPallini 2,598
3 Richard MacCutchan 2,025
4 Abhinav S 1,788


Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 25 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100