0

I want to edit the path in a link file which leeds to a file or folder which changes it's path quite often. I found some things in C or other languages but never for C#.

Test.lnk -> C:\TestFolder 1.2.3\
I want to change that link using C# to
Test.lnk -> C:\TestFolder 1.2.4\

Does anyone know how to do so?

theknut
  • 2,533
  • 5
  • 26
  • 41
  • Check out the top answer for the following question: http://stackoverflow.com/questions/234231/creating-application-shortcut-in-a-directory. – Jalayn Feb 14 '12 at 10:53
  • Is there nothing native? Furthermore I need something also working on WinXP. They only tried Win Server 2008 or later. – theknut Feb 14 '12 at 11:02

1 Answers1

1

I don't think it's possible to edit the path in a link file. Instead you can delete the old shortcut and create a new one using the COM Windows Script Host Object Model:

using System;
using IWshRuntimeLibrary;

namespace ShortCutTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var wsh = new WshShell();
            var shortcut = (IWshShortcut)wsh.CreateShortcut(@"C:\cmd.lnk");
            shortcut.Description = "Shortcut for cmd.exe";
            shortcut.TargetPath = Environment.GetFolderPath(Environment.SpecialFolder.System) +  @"\cmd.exe";
            shortcut.Save();
        }
    }
}

As far as I know there is no native way in .NET to do that.

theknut
  • 2,533
  • 5
  • 26
  • 41
Michael Alves
  • 635
  • 1
  • 7
  • 18