2016-10-21 4 views
0

次のコードを使用して、いくつかのファイルのショートカットを作成し、スタートアップフォルダにコピーしています。このプログラムはWindows XP上で動作し、.NET Framework 2.0を使用して構築されています。私はショートカットからファイルを起動した場合 が、私はプロセスでそれを見ることができますが、私はそれから任意の測定値を持つことはできません。ショートカットコードC#ショートカットエラーを作成する

try 
{ 
    object shDesktop = (object)"Desktop"; 
    WshShell shell = new WshShell(); 
    //Shorcut name 
    string shortcutAddress = @"C:\Documents and Settings\Astrophysics Inc\Start Menu\Programs\Startup\Shortcut to " + s + ".lnk"; 
    IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress); 
    shortcut.Description = "New shortcut for a Notepad"; 
    shortcut.Hotkey = "Ctrl+Shift+N"; 
    //File path 
    shortcut.TargetPath = @"C:\Program Files\" +path+ @"\" + s + ".exe"; 
    shortcut.Save(); 
} 
catch (Exception) { }; 

を作成

だからエラーは次のとおりです。しかし、私はプロパティに移動し、ショートカットの場所(実際のファイル)を見つけて、それ自身のフォルダから開くと、アプリケーションが動作します。 同じショートカットを物理的に作成しようとしましたが(C#を使用していない)、意図したとおりに動作します。

あなたはこの問題がどのような原因であると思われますか?それを数回実行しようとして、同じ問題を抱えています。そして、はい、私はショートカットが同じファイルにリンクされていることを確信しています。

+0

私はXPではありませんが、それは私によって動作しています。それはWorkingDirectoryプロパティセットとどのように反応しますか? –

+0

それは私のWindows 7でも完璧に動作します。 さらに、私はエラーが発生しない、ショートカットが作成され、ファイルが起動されているが、この特定のショートカットから実行したときに正しく機能していない。私は作業ディレクトリのプロパティを設定しようとし、あなたに教えてくれます。 –

+0

@JedBurke作業ディレクトリを追加して問題を修正しました!ありがとう! –

答えて

0

これを試してください。

string DirectoryPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup); 
      string TargetPathName = Application.ExecutablePath; // Exe Directory here I have set it to my own application dir 
      string LinkPathName = "You Exe Name"; 

      // Get some file and directory information 
      DirectoryInfo SpecialDir=new DirectoryInfo(DirectoryPath); 
      // First get the filename for the original file and create a new file 
      // name for a link in the Startup directory 
      // 
      FileInfo OriginalFile = new FileInfo(LinkPathName); 
      string NewFileName = SpecialDir.FullName+"\\"+OriginalFile.Name+".lnk"; 
      FileInfo LinkFile = new FileInfo(NewFileName); 

      if (LinkFile.Exists) return; 

      try 
      { 
       // Create a shortcut in the special folder for the file 
       // Making use of the Windows Scripting Host 
       WshShell shell = new WshShell(); 
       IWshShortcut link = (IWshShortcut)shell.CreateShortcut(LinkFile.FullName); 
       link.TargetPath = TargetPathName; 
       link.Save(); 
      } 
      catch 
      { 
       MessageBox.Show("Unable to create link in directory: " + NewFileName, 
        "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      } 
関連する問題