Search code examples
c#visual-studiopppoe

C# howto Create Network Dialer shortcut on desktop


I have created a code in Visual C# using DOTRAS library, which creates pppoe dialer in network connection. But I am unable to figure out how to create the dialer shortcut on the desktop. I know in general howto create application shortcut in c# but unable to get the network connection shortcut code. Any suggestions would be appreciable.


Solution

  • This is the best i know:

    string destDir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
    string destFileName = @"Connect To Example.lnk";
    
    // Create .lnk file
    string path = System.IO.Path.Combine(destDir, destFileName);
    FileStream fs = File.Create(path);
    fs.Close();
    
    // Instantiate a ShellLinkObject that references the .lnk we created
    Shell32.Shell shell = new Shell32.Shell();
    Shell32.Folder shellFolder = shell.NameSpace(destDir);
    Shell32.FolderItem shellFolderItem = shellFolder.Items().Item(destFileName);
    Shell32.ShellLinkObject shellLinkObject = (Shell32.ShellLinkObject)shellFolderItem.GetLink;
    
    // Set .lnk properties
    shellLinkObject.Arguments = "-d Example";
    shellLinkObject.Description = "Example Connection";
    shellLinkObject.Path = @"%windir%\System32\rasphone.exe";
    shellLinkObject.WorkingDirectory = "%windir%";
    
    shellLinkObject.Save(path);
    

    Replace "Example" with your connection name