4

IWshRuntimeLibraryを使用してc#でショートカットを作成しています。ショートカットファイル名はヒンディー語「नमस्ते」です。

私は次のコードを使用してショートカットを作成しています。shortcutName = "नमस्ते.lnk"

 WshShellClass wshShell = new WshShellClass();
 IWshRuntimeLibrary.IWshShortcut shortcut;

shortcut = (IWshRuntimeLibrary.IWshShortcut)wshShell.CreateShortcut(destPath + "\\" + shortcutName);

 shortcut.TargetPath = sourcePath;
 shortcut.Save();

shortcut.Save()次の例外が発生しています。

The filename, directory name, or volume label syntax is incorrect. (Exception from HRESULT: 0x8007007B)
4

1 に答える 1

6

デバッガーのどこが悪いのかがわかります。デバッガーで「ショートカット」を調べて、ヒンディー語の名前が疑問符に置き換えられていることに注意してください。これは無効なファイル名を生成し、例外をトリガーします。

文字列を処理できない古いスクリプトサポートライブラリを使用しています。より最新のものを使用する必要があります。プロジェクト+参照の追加、[参照]タブで、c:\ windows \ system32\shell32.dllを選択します。これにより、シェル関連の作業を行うためのいくつかのインターフェイスを備えたShell32名前空間がプロジェクトに追加されます。これを実現するのに十分なだけ、ShellLinkObjectインターフェイスを使用して.lnkファイルのプロパティを変更できます。1つのトリックが必要です。それは、新しい.lnkファイルを最初から作成する機能がありません。空の.lnkファイルを作成することでこれを解決します。これはうまくいきました:

    string destPath = @"c:\temp";
    string shortcutName = @"नमस्ते.lnk";

    // Create empty .lnk file
    string path = System.IO.Path.Combine(destPath, shortcutName);
    System.IO.File.WriteAllBytes(path, new byte[0]);
    // Create a ShellLinkObject that references the .lnk file
    Shell32.Shell shl = new Shell32.Shell();
    Shell32.Folder dir = shl.NameSpace(destPath);
    Shell32.FolderItem itm = dir.Items().Item(shortcutName);
    Shell32.ShellLinkObject lnk = (Shell32.ShellLinkObject)itm.GetLink;
    // Set the .lnk file properties
    lnk.Path = Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\notepad.exe";
    lnk.Description = "nobugz was here";
    lnk.Arguments = "sample.txt";
    lnk.WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    lnk.Save(path);
于 2012-11-24T15:12:03.597 に答える