8 Replies - 1388 Views - Last Post: 26 January 2015 - 09:43 AM Rate Topic: -----

#1 stevenryals  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 07-July 14

Programatically Add New Custom Network Location to "My Computer

Posted 22 January 2015 - 01:49 PM

I have a need to add a network location to users computers, but Can't seem to get it going.

Any ideas?

It will be a simple windows form with a button for users to add the network location..
.net 4.5
VB

Thanks for any ideas you may have..

Here's what I have now..

 objWSHShell = CreateObject("Wscript.Shell")
        objShell = CreateObject("Shell.Application")

        objFolder = objShell.Namespace(NETHOOD)
        objFolderItem = objFolder.Self
        strNetHood = objFolderItem.Path

        strShortcutName = "Sherlock1"
        strShortcutPath = "\\NETWORKPATH.com\Tech\dir"

        objShortcut = objWSHShell.CreateShortcut _
        (strNetHood & "\" & strShortcutName & ".lnk")
        objShortcut.TargetPath = strShortcutPath
        objShortcut.Save()

This post has been edited by stevenryals: 22 January 2015 - 01:49 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Programatically Add New Custom Network Location to "My Computer

#2 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6541
  • View blog
  • Posts: 26,474
  • Joined: 12-December 12

Re: Programatically Add New Custom Network Location to "My Computer

Posted 22 January 2015 - 02:39 PM

What's wrong with your code? Is there an error message?
Was This Post Helpful? 0
  • +
  • -

#3 stevenryals  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 07-July 14

Re: Programatically Add New Custom Network Location to "My Computer

Posted 22 January 2015 - 03:12 PM

No error, it just simply doesn't add the location.. ..
Was This Post Helpful? 0
  • +
  • -

#4 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1490
  • View blog
  • Posts: 3,772
  • Joined: 01-February 13

Re: Programatically Add New Custom Network Location to "My Computer

Posted 22 January 2015 - 08:56 PM

Hi,

Have you verified that you are getting the NETHOOD folder path by checking the strNetHood string?

Have you also verified that the "\\NETWORKPATH.com\Tech\dir" is a valid path? You can go to the desktop and copy an existing shortcut or create a new one and in its properties type that path in the Target Path spot. I know on XP it will tell me if the path is not valid.

One last thing is, have you tried saving the shortcut to another place like the desktop or somewhere you know for sure you will have access to write files. Try saving it elsewhere once or try writing a simple text file to the NETHOOD folder.

I don`t have a network going right now to test it on but, the code below will create a shortcut named Sherlock1 in the NETHOOD folder for my "C:TestFolder".

I used a bit different of a way to get the NETHOOD folder path than you.

'After starting your new project go to the VB menu and click (Project) then click (Add Refference...)
'When the window opens click the (Com) tab and scroll down and doubleclick (Windows Scripting Host Object Model).

Option Strict On
Imports IWshRuntimeLibrary

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim myShell As New WshShell()
        Dim fldrs As IWshCollection = myShell.SpecialFolders
        Dim strNetHood As String = ""

        For x As Integer = 0 To fldrs.length - 1
            strNetHood = fldrs(x).ToString
            If strNetHood.ToLower.Contains("nethood") Then Exit For 'found the NETHOOD folder so exit loop
        Next

        Dim strShortcutName As String = IO.Path.Combine(strNetHood, "Sherlock1.lnk")
        Dim strTargetPath As String = "C:\TestFolder" ' "\\NETWORKPATH.com\Tech\dir"

        Dim myShortcut As WshShortcut = CType(myShell.CreateShortcut(strShortcutName), WshShortcut)
        myShortcut.TargetPath = strTargetPath
        myShortcut.Save()
    End Sub
End Class


This post has been edited by IronRazer: 26 January 2015 - 09:59 AM

Was This Post Helpful? 1
  • +
  • -

#5 stevenryals  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 07-July 14

Re: Programatically Add New Custom Network Location to "My Computer

Posted 26 January 2015 - 07:55 AM

Thanks, that's got to be my problem. I'm not obtaining the correct Nethood..

I must not have IWshRuntimeLibrary installed, what library is that? I searched in nuget but found nothing.
Was This Post Helpful? 0
  • +
  • -

#6 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1490
  • View blog
  • Posts: 3,772
  • Joined: 01-February 13

Re: Programatically Add New Custom Network Location to "My Computer

Posted 26 January 2015 - 09:12 AM

Hi,

Sorry for the delay.

I wrote a note at the top of the code that you need to add a reference to the "Windows Scripting Host Object Model" on the Com tab of the Add Reference dialog window.

I am still on VS2008 so, i am not sure if they have changed the dialog in any way.
Was This Post Helpful? 1
  • +
  • -

#7 stevenryals  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 07-July 14

Re: Programatically Add New Custom Network Location to "My Computer

Posted 26 January 2015 - 09:15 AM

Great thanks! it does add the shortcut now that I'm correctly defining nethood.

However, the shortcut doenst show up where I'd like it (in the left pane of the explorer window under 'computer' as a shortcut to the shortcut).. but once I click 'computer' it does appear. So, it seems I'm in search of creating a folder and then adding the link property to the folder... which, according to the research I've done so far, is not an easy thing to do.

Thanks for your help sir.
Was This Post Helpful? 0
  • +
  • -

#8 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1490
  • View blog
  • Posts: 3,772
  • Joined: 01-February 13

Re: Programatically Add New Custom Network Location to "My Computer

Posted 26 January 2015 - 09:34 AM

Yea, i did not do a lot of research on creating a shortcut in the NetHood folder the other day. I basically just threw it together and looked in the NetHood folder to see that it was added.

I am on an older XP machine running as an admin so, i can only imagine that things may be different on the newer OS`s. They may have more security and probably have things set up differently than my old machine. If i find anything that might be helpful i will let you know.

However, someone else may still have some suggestions too.
Was This Post Helpful? 0
  • +
  • -

#9 stevenryals  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 07-July 14

Re: Programatically Add New Custom Network Location to "My Computer

Posted 26 January 2015 - 09:43 AM

Thanks again. I'll keep plugging away.. but the current solution will work, i'll just have to do a bit more one on one counseling/training to show them how to find the shortcut..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1