I am writing windows program to create shortcuts and having some difficult time.
The problem that I am facing is with special characters. When my path has special characters like "ü" and "Ş" program fails to create shortcut and gives an error message.
System.IO.DirectoryNotFoundException: The system cannot find the path specified.
I did further analysis and here is what happen,
If my path has a folder "Ravi - Ş" Shortcut API tries to create a file under "Ravi - S" folder which is little weird. Obviously, the folder does not exist and fails to create a shortcut. If I manually create the folder with "Ravi - S" and then run the program, shortcut created successfully under "Ravi - S" folder which is not the expected behavior.
So what I feel that Shortcut API is doing something extra which replaces these special characters.
Is it possible to create shortcuts with these special characters?
string MY_DOCUMENTS_PATH = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
path = string.Format("C:\\Ravi\\Ravi - Ş\\test.lnk", MY_DOCUMENTS_PATH);
networkPath = @"\\ravi.companyname.com\urlpart1\urlpart2\";
description = @"link description";
CreateLink(path, networkPath, description);
Below is the function to create shortcuts.
private static void CreateLink(string path, string networkPath, string description)
{
try
{
if (Directory.Exists(Path.GetDirectoryName(path)))
{
Console.WriteLine("Path exists");
}
else
{
Console.WriteLine("Path does not exists");
}
IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell();
IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(path);
shortcut.Description = description;
shortcut.TargetPath = networkPath;
shortcut.Save();
}
catch (Exception ex)
{
Console.WriteLine("Error while creating shortcut");
Console.WriteLine(ex.ToString());
}
}