none
WshRuntimeLibrary TargetPath problem with Cyrillic chars RRS feed

  • Question

  • Hi,

    I tried to create a shortcut with Cyrillic chars in path. I got

    System.ArgumentException : Value does not fall within the expected range.

    at IWshRuntimeLibrary.IWshShortcut.set_TargetPath(Stringout_Path)

    The code is pretty straightforward :

    using IWshRuntimeLibrary;

     public static void CreateFileLink( String sourceFilePath, String linkDestinationFolderPath )
            {
                var wsh = new IWshShell_Class();
                FileInfo fileInfo = new FileInfo( sourceFilePath );
                var shortcut = wsh.CreateShortcut( String.Format("{0}\\{1}.lnk",linkDestinationFolderPath,fileInfo.Name) ) as IWshShortcut;
                if( shortcut != null )
                {
                    shortcut.TargetPath = sourceFilePath;
                    shortcut.WindowStyle = 1;
                    shortcut.Save();
                }
                else
                {
                    log.WarnFormat(
                        "ShortCut couldn't be created for {0} at {1}", sourceFilePath, linkDestinationFolderPath );
                }
            }

    [Test]

            public void TestShortCutWithCyrillicCreation()
            {
                CreateFileLink(Environment.CurrentDirectory + "\\28.06.чай\\_malina4.jpg", Environment.CurrentDirectory);

                Assert.IsTrue(File.Exists(Environment.CurrentDirectory + "\\_malina4.jpg.lnk"));
            }

     Did anybody encounter similar issue? Thanks!


    Max

    Thursday, November 28, 2013 6:42 AM

Answers

  • Hi Max.Terl,

    I think it is not a Dev issue now, since we have successfully writen the content into the file.

    I believe you will get same problem when you create the file manually. So I think it will be better to ask help from window forum. For example: Windows 7 Miscellaneous

    Thanks for your understanding.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    Alan Yao
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, December 6, 2013 6:11 AM

All replies

  • Hi Max,

    The sourceFilePath argument of CreateFileLink contains illegal charecters “чай”, you should change it to a correct one like below:

    CreateFileLink("e:\\Code First Migrations.pptx", "e:\\");

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, November 28, 2013 9:24 AM
    Moderator
  • Hi. The problem is that those Cyrillic chars are actually a valid folder
    name. So how do I make a shortcut to a file residing in a dir that has
    Cyrillic chars ?


    Max

    Thursday, November 28, 2013 10:59 AM
  • Hi Max,

    Please try this solution, it works on my side. Please let me know the result.

    private void button1_Click(object sender, EventArgs e)

            {

                string linkName = "shortcut of directory";

                string deskDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

                using (StreamWriter writer = new StreamWriter(deskDir + "\\" + linkName + ".url"))

                {

                    string app = "e:\\28.06.чай";

                    writer.WriteLine("[InternetShortcut]");

                    writer.WriteLine("URL=file:///" + app);

                    writer.WriteLine("IconIndex=1");

                    string icon = app.Replace('\\', '/');

                    writer.WriteLine("IconFile=" + icon);

                    writer.Flush();

                }

                MessageBox.Show("success");

            }

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, November 29, 2013 8:51 AM
    Moderator
  • Hi,

    Well, I didn't see at your code usage of the actual file name. So I tried that one:

    public static void CreateFileLink2(String sourceFilePath, String linkDestinationFolderPath)
            {
                var fileInfo = new FileInfo(sourceFilePath);
                using ( StreamWriter writer = new StreamWriter(linkDestinationFolderPath + "\\" + fileInfo.Name + ".url") )
                {
                    writer.WriteLine("[InternetShortcut]");

                    writer.WriteLine("URL=file:///" + fileInfo.FullName);

                    writer.WriteLine("IconIndex=1");

                    string icon = fileInfo.FullName.Replace('\\', '/');

                    writer.WriteLine("IconFile=" + icon);

                    writer.Flush();

                }
            }

    But it didn't work. The resulting Internet Shortcut file is

    [InternetShortcut]
    URL=file:///C:\Src\AdsAutoTestTool2\QA_IAB_Test\bin\Debug\28.06.чай\_malina4.jpg
    IconIndex=1
    IconFile=C:/Src/AdsAutoTestTool2/QA_IAB_Test/bin/Debug/28.06.чай/_malina4.jpg

    But when I click on it I get "Windows cannot find 'C:\Src\AdsAutoTestTool2\QA_IAB_Test\bin\Debug\28.06'. Make sure you typed the name correctly, and then try again" message. It seems that for some reasons the ".чай\_malina4.jpg" is cut from the url when trying to go to the address.


    Max


    • Edited by Max.Terl Sunday, December 1, 2013 6:51 AM
    Sunday, December 1, 2013 6:50 AM
  • Hi Max.Terl,

    I think it is not a Dev issue now, since we have successfully writen the content into the file.

    I believe you will get same problem when you create the file manually. So I think it will be better to ask help from window forum. For example: Windows 7 Miscellaneous

    Thanks for your understanding.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    Alan Yao
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, December 6, 2013 6:11 AM