static void CreateShortcut(string FullFolderPath) { string FolderName = Path.GetFileNameWithoutExtension(FullFolderPath); string FolderDir = Path.GetDirectoryName(FullFolderPath); string FolderRoot = Path.GetPathRoot(FullFolderPath); string lnkPath = Path.Combine(FolderDir, FolderName) + ".lnk"; File.WriteAllBytes(lnkPath, new byte[] { }); Shell32.Shell shl = new Shell32.ShellClass(); Shell32.Folder dir = shl.NameSpace(FolderDir); Shell32.FolderItem itm = dir.Items().Item(FolderName + ".lnk"); Shell32.ShellLinkObject lnk = (Shell32.ShellLinkObject)itm.GetLink; lnk.Path = FolderRoot + "\\execute.bat"; lnk.Description = FolderName; lnk.Arguments = "\"" + FullFolderPath + "\""; lnk.WorkingDirectory = FolderDir; SHFILEINFO shinfo = new SHFILEINFO(); Win32.SHGetFileInfo(FullFolderPath, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), (int)0x1000); lnk.SetIconLocation(shinfo.szDisplayName, shinfo.iIcon); lnk.Save(lnkPath); }
private static void ChangeLinkTarget(string shortcutFullPath, string newTarget) { // Load the shortcut. Shell32.Folder folder = GetShell32NameSpaceFolder(Path.GetDirectoryName(shortcutFullPath)); Shell32.FolderItems items = folder.Items(); IEnumerator en = items.GetEnumerator(); Shell32.FolderItem item = null; while (en.MoveNext()) { item = en.Current as Shell32.FolderItem; if (item == null) { continue; } if (item.Name == Path.GetFileName(shortcutFullPath)) { break; } } if (item == null || !item.IsLink) { return; } Shell32.ShellLinkObject currentLink = (Shell32.ShellLinkObject)item.GetLink; // Assign the new path here. This value is not read-only. currentLink.Path = newTarget; // Save the link to commit the changes. currentLink.Save(); }
static void Main(string[] args) { WshShell shell = new WshShell(); var wsh = new IWshShell_Class(); int count = 1; var sourcePath = "G:\\2.Anime\\pixivutil"; var targetPath = "C:\\Users\\mouri\\Downloads\\test\\___"; Random rnd = new Random(); var parentDirectory = new DirectoryInfo(sourcePath); foreach (var directory in parentDirectory.EnumerateDirectories()) { if (directory.FullName == targetPath) { continue; } FileInfo[] randomFiles = directory .GetFiles("*.*", SearchOption.AllDirectories) .Where(file => !file.Name.Contains("folder")) .OrderByDescending(x => x.Name) .Take(20) .OrderBy(x => rnd.Next()) .Take(count) .ToArray(); foreach (FileInfo file in randomFiles) { string targetFile = Path.Combine(targetPath, file.Name); //Console.WriteLine("copy " + file.FullName + " -> " + targetFile); //file.CopyTo(targetFile); //IWshRuntimeLibrary.IWshShortcut shortcut = wsh.CreateShortcut(targetFile + ".lnk") as IWshRuntimeLibrary.IWshShortcut; //string shortcutAddress = targetFile + @".lnk"; //IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress); //shortcut.TargetPath = file.FullName; //shortcut.Save(); var shortcutName = file.Name + ".lnk"; // Create empty .lnk file string path = System.IO.Path.Combine(targetPath, 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(targetPath); Shell32.FolderItem itm = dir.Items().Item(shortcutName); Shell32.ShellLinkObject lnk = (Shell32.ShellLinkObject)itm.GetLink; // Set the .lnk file properties lnk.Path = file.FullName; lnk.Save(path); } } }
private static void ChangeLinkTarget(string shortcutFullPath, string newTarget) { // Load the shortcut. Shell32.Shell shell = new Shell32.Shell(); Shell32.Folder folder = shell.NameSpace(Path.GetDirectoryName(shortcutFullPath)); Shell32.FolderItem folderItem = folder.Items().Item(Path.GetFileName(shortcutFullPath)); Shell32.ShellLinkObject currentLink = (Shell32.ShellLinkObject)folderItem.GetLink; // Assign the new path here. This value is not read-only. currentLink.Path = newTarget; // Save the link to commit the changes. currentLink.Save(); }
static void createLink(string path, string name) {//c:\windows\system32\shell32.dll Shell32.Shell shl = new Shell32.Shell(); StreamWriter sw = new StreamWriter(path + "\\" + name + ".lnk", false); sw.Close(); Shell32.Folder dir = shl.NameSpace(path); Shell32.FolderItem itm = dir.Items().Item(name + ".lnk"); Shell32.ShellLinkObject lnk = (Shell32.ShellLinkObject)itm.GetLink; lnk.Path = Application.ExecutablePath; lnk.Description = "pżyłożeńe"; lnk.Arguments = ""; lnk.WorkingDirectory = Application.ExecutablePath.ToString().Substring(0, Application.ExecutablePath.ToString().LastIndexOf('\\')); lnk.SetIconLocation(Application.ExecutablePath, 0); lnk.Save(); }
private bool CreateShortcut(string filePath, string targetPath) { if (string.IsNullOrEmpty(filePath) || string.IsNullOrEmpty(targetPath)) { return(false); } if (!System.IO.File.Exists(filePath)) { if (!Directory.Exists(filePath)) { return(false); } return(CreateDirectoryShortcut(filePath, targetPath)); } try { var file = new FileInfo(filePath); CreateDirectoryIfNotExist(targetPath); // Create empty .lnk file var fileName = string.IsNullOrEmpty(targetFileNameTextBox.Text) ? file.Name : targetFileNameTextBox.Text; var shortcutName = fileName + ".lnk"; string path = Path.Combine(targetPath, 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(targetPath); Shell32.FolderItem itm = dir.Items().Item(shortcutName); Shell32.ShellLinkObject lnk = (Shell32.ShellLinkObject)itm.GetLink; // Set the .lnk file properties lnk.Path = file.FullName; lnk.Save(path); } catch (Exception ex) { MessageBox.Show(ex.Message); return(false); } return(true); }
private void traiterSimulationFichierEnCours(String shortcutFullPath, FormAffichage fa) { // Load the shortcut. Shell32.Shell shell = new Shell32.Shell(); Shell32.Folder folder = shell.NameSpace(Path.GetDirectoryName(shortcutFullPath)); Shell32.FolderItem folderItem = folder.Items().Item(Path.GetFileName(shortcutFullPath)); Shell32.ShellLinkObject currentLink = (Shell32.ShellLinkObject)folderItem.GetLink; if (currentLink.Path.Contains(comboBoxTxt2Change.Text)) { fa.textBox1.AppendText(Environment.NewLine); fa.textBox1.AppendText(currentLink.Path); currentLink.Save(); } }
public void ChangeLinkTarget(string shortcutFullPath) { // ToDo verif combo pas vide si possible avant // ToDo sauver .lnk avant changement // Load the shortcut. Shell32.Shell shell = new Shell32.Shell(); Shell32.Folder folder = shell.NameSpace(Path.GetDirectoryName(shortcutFullPath)); Shell32.FolderItem folderItem = folder.Items().Item(Path.GetFileName(shortcutFullPath)); Shell32.ShellLinkObject currentLink = (Shell32.ShellLinkObject)folderItem.GetLink; if (currentLink.Path.Contains(comboBoxTxt2Change.Text)) { // ici creer repertoire sauvegarde selon etat marqeur ou test existance String sRepSauvegardeAncien = Path.Combine(comboBoxWorkingDirectory.Text, "sauveOldLNK"); String sRepSauvegardeNouveau = Path.Combine(comboBoxWorkingDirectory.Text, "sauveNewLNK"); String sNomFichier = Path.GetFileName(shortcutFullPath); String sDirectoryPath = Path.GetDirectoryName(shortcutFullPath); if (!Directory.Exists(sRepSauvegardeAncien)) { Directory.CreateDirectory(sRepSauvegardeAncien); } if (!Directory.Exists(sRepSauvegardeNouveau)) { Directory.CreateDirectory(sRepSauvegardeNouveau); } // sauvegarder le lnk File.Copy(shortcutFullPath, sRepSauvegardeAncien + "\\" + sNomFichier, true); //changer le repertoire de travail currentLink.WorkingDirectory = currentLink.WorkingDirectory.Replace(comboBoxTxt2Change.Text, comboBoxNouveauPrefix.Text); // Assign the new path here. This value is not read-only. currentLink.Path = currentLink.Path.Replace(comboBoxTxt2Change.Text, comboBoxNouveauPrefix.Text); // Save the link to commit the changes. currentLink.Save(); File.Copy(shortcutFullPath, sRepSauvegardeNouveau + "\\" + sNomFichier, true); } //fermer le liens ? }
public bool CreateShortcut(string sLinkFile, string sTargetFile, string sArguments = "", string sDescription = "", string sWorkingDir = "") { // Source: http://www.vbarchiv.net/tipps/details.php?id=1601 // @TODO: Find alternative try { Shell32.Shell oShell = new Shell32.Shell(); Shell32.Folder oFolder = null; Shell32.ShellLinkObject oLink = null; string sPath = sLinkFile.Substring(0, sLinkFile.LastIndexOf(Path.DirectorySeparatorChar)); string sFile = Path.GetFileName(sLinkFile); short F = Convert.ToInt16(Microsoft.VisualBasic.FileSystem.FreeFile()); Microsoft.VisualBasic.FileSystem.FileOpen(F, sLinkFile, Microsoft.VisualBasic.OpenMode.Output); Microsoft.VisualBasic.FileSystem.FileClose(F); oFolder = oShell.NameSpace(sPath); oLink = (Shell32.ShellLinkObject)oFolder.Items().Item(sFile).GetLink; if (sArguments.Length > 0) { oLink.Arguments = sArguments; } if (sDescription.Length > 0) { oLink.Description = sDescription; } if (sWorkingDir.Length > 0) { oLink.WorkingDirectory = sWorkingDir; } oLink.Path = sTargetFile; oLink.Save(); oLink = null; oFolder = null; oShell = null; return(true); } catch (Exception) { if (File.Exists(sLinkFile)) { File.Delete(sLinkFile); } return(false); } }
public static void CreateShortcut(string shortcutName, string shortcutPath, string targetFileLocation) { string shortcutLocation = Path.Combine(shortcutPath, shortcutName); // Create empty .lnk file File.WriteAllBytes(shortcutName, new byte[0]); // Create a ShellLinkObject that references the .lnk file Shell32.Shell shl = new Shell32.Shell(); Shell32.Folder dir = shl.NameSpace(Path.GetDirectoryName(shortcutLocation)); Shell32.FolderItem itm = dir.Items().Item(shortcutName); Shell32.ShellLinkObject lnk = (Shell32.ShellLinkObject)itm.GetLink; // Set the .lnk file properties lnk.Path = targetFileLocation; lnk.Arguments = "-c \"" + Path.GetFileNameWithoutExtension(shortcutName) + ".ini\""; lnk.WorkingDirectory = Path.GetDirectoryName(targetFileLocation); lnk.Save(shortcutName); }
private void CreateShortcut(object sender, RoutedEventArgs e) { var desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); var lnkName = "FL Studio (skinned)"; string lnkPath = System.IO.Path.Combine(desktopFolder, lnkName) + ".lnk"; System.IO.File.WriteAllBytes(lnkPath, new byte[] { }); Shell32.Shell shl = new Shell32.ShellClass(); Shell32.Folder dir = shl.NameSpace(desktopFolder); Shell32.FolderItem itm = dir.Items().Item(lnkName + ".lnk"); Shell32.ShellLinkObject lnk = (Shell32.ShellLinkObject)itm.GetLink; lnk.Path = System.Reflection.Assembly.GetExecutingAssembly().Location; lnk.Arguments = "-nogui"; lnk.WorkingDirectory = System.IO.Path.GetDirectoryName(lnk.Path); lnk.SetIconLocation(string.Format(@"{0}\FL64.exe", Config.current.flStudioPath), 0); lnk.Save(lnkPath); }
private void Form1_DragDrop(object sender, DragEventArgs e) { //IWshShell wsh = new WshShellClass(); Shell32.Shell shell = new Shell32.Shell(); string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); var shortcuts = files.Where(f => f.EndsWith(".lnk")).ToList(); foreach (var s in shortcuts) { Shell32.Folder folder = shell.NameSpace(Path.GetDirectoryName(s)); Shell32.FolderItem folderItem = folder.Items().Item(Path.GetFileName(s)); Shell32.ShellLinkObject currentLink = (Shell32.ShellLinkObject)folderItem.GetLink; // Assign the new path here. This value is not read-only. currentLink.Path = currentLink.Path.Replace(tbSearch.Text, tbReplace.Text); currentLink.WorkingDirectory = currentLink.WorkingDirectory.Replace(tbSearch.Text, tbReplace.Text); // Save the link to commit the changes. currentLink.Save(); } //foreach (var f in shortcuts) MessageBox.Show(f); }
public static void SetLnkTarget(string shortcutFullPath, string newTarget) { try { // Load the shortcut. Shell32.Shell shell = new Shell32.Shell(); Shell32.Folder folder = (Shell32.Folder)shell.NameSpace(Path.GetDirectoryName(shortcutFullPath)); Shell32.FolderItem folderItem = folder.Items().Item(Path.GetFileName(shortcutFullPath)); Shell32.ShellLinkObject currentLink = (Shell32.ShellLinkObject)folderItem.GetLink; //Assign the new path here. This value is not read-only. currentLink.Path = newTarget; // Save the link to commit the changes. currentLink.Save(); } catch (Exception Ex) { Console.WriteLine(Ex.Message); } }