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

윈도우 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); 
      }