예제 #1
0
        //http://csharphelper.com/blog/2018/06/get-information-about-windows-shortcuts-in-c/
        internal static string GetShortcutInfo(string full_name,
                                               out string name, out string path, out string descr,
                                               out string working_dir, out string args)
        {
            name        = "";
            path        = "";
            descr       = "";
            working_dir = "";
            args        = "";
            try
            {
                // Make a Shell object.
                Shell32.Shell shell = new Shell32.Shell();

                // Get the shortcut's folder and name.
                string shortcut_path =
                    full_name.Substring(0, full_name.LastIndexOf("\\"));
                string shortcut_name =
                    full_name.Substring(full_name.LastIndexOf("\\") + 1);
                if (!shortcut_name.EndsWith(".lnk"))
                {
                    shortcut_name += ".lnk";
                }

                // Get the shortcut's folder.
                Shell32.Folder shortcut_folder =
                    shell.NameSpace(shortcut_path);

                // Get the shortcut's file.
                Shell32.FolderItem folder_item =
                    shortcut_folder.Items().Item(shortcut_name);

                if (folder_item == null)
                {
                    return("Cannot find shortcut file '" + full_name + "'");
                }
                if (!folder_item.IsLink)
                {
                    return("File '" + full_name + "' isn't a shortcut.");
                }

                // Display the shortcut's information.
                Shell32.ShellLinkObject lnk =
                    (Shell32.ShellLinkObject)folder_item.GetLink;
                name        = folder_item.Name;
                descr       = lnk.Description;
                path        = lnk.Path;
                working_dir = lnk.WorkingDirectory;
                args        = lnk.Arguments;
                return("");
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }
예제 #2
0
 public static IEnumerable <string> GetContents()
 {
     Shell = new Shell();
     Shell32.Folder RecycleBin = Shell.NameSpace(10);
     foreach (FolderItem2 Entry in RecycleBin.Items())
     {
         yield return(Entry.Path);
     }
     Marshal.FinalReleaseComObject(Shell);
 }
예제 #3
0
        private static bool SetNetworkAdapter(bool status)
        {
            const string discVerb          = "停用(&B)";     // "停用(&B)";
            const string connVerb          = "启用(&A)";     // "启用(&A)";
            const string network           = "网络连接";       //"网络连接";
            const string networkConnection = "以太网适配器 以太网"; // "本地连接"
            string       sVerb             = null;

            if (status)
            {
                sVerb = connVerb;
            }
            else
            {
                sVerb = discVerb;
            }
            Shell32.Shell  sh     = new Shell32.Shell();
            Shell32.Folder folder = sh.NameSpace(Shell32.ShellSpecialFolderConstants.ssfCONTROLS);
            try
            {
                //进入控制面板的所有选项
                foreach (Shell32.FolderItem myItem in folder.Items())
                {
                    //进入网络连接
                    if (myItem.Name == network)
                    {
                        Shell32.Folder fd = (Shell32.Folder)myItem.GetFolder;
                        foreach (Shell32.FolderItem fi in fd.Items())
                        {
                            //找到本地连接
                            if ((fi.Name == networkConnection))
                            {
                                //找本地连接的所有右键功能菜单
                                foreach (Shell32.FolderItemVerb Fib in fi.Verbs())
                                {
                                    if (Fib.Name == sVerb)
                                    {
                                        Fib.DoIt();
                                        return(true);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return(false);
            }
            return(true);
        }
예제 #4
0
        // Undos the previous moves and deletions
        private void UndoMove()
        {
            if (_movedItems.Count == 0)
            {
                return;
            }
            _changed = true;

            try {
                DisplayItem fileToUndo = _movedItems.ElementAt(0);

                if (fileToUndo.HasBeenDeleted)
                {
                    // Based on
                    // https://stackoverflow.com/questions/6025311/how-to-restore-files-from-recycle-bin?lq=1

                    string         Item     = fileToUndo.GetFilePath().Replace(@"\\", @"\"); // restore is sensitive to double backslashes
                    Shell          Shl      = new Shell();
                    Shell32.Folder Recycler = Shl.NameSpace(10);
                    foreach (FolderItem FI in Recycler.Items())
                    {
                        string FileName = Recycler.GetDetailsOf(FI, 0);
                        if (Path.GetExtension(FileName) == "")
                        {
                            FileName += Path.GetExtension(FI.Path);
                        }
                        //Necessary for systems with hidden file extensions.
                        string FilePath = Recycler.GetDetailsOf(FI, 1);
                        if (Item == Path.Combine(FilePath, FileName))
                        {
                            File.Move(FI.Path, fileToUndo.GetFilePath());
                            fileToUndo.HasBeenDeleted = false;
                            break;
                        }
                    }
                }
                else
                {
                    File.Move(fileToUndo.GetFilePath(), fileToUndo.GetOldFilePath());
                    fileToUndo.SetFilePath(fileToUndo.GetOldFilePath());
                }

                _displayItems.Insert(_displayedItemIndex, fileToUndo);
                isInCache.Insert(_displayedItemIndex, false);
                _movedItems.RemoveAt(0);
            }
            catch
            {
                Interaction.MsgBox("File is currently being used by another program or has been removed");
            }

            UpdateContent();
        }
예제 #5
0
        /// <summary>
        /// method for colled info or delete files
        /// </summary>
        /// <param name="flag">flag delete</param>
        /// <returns>information</returns>
        public string CallClear(bool flag)
        {
            Shell shell = new Shell();

            Shell32.Folder Recycler = shell.NameSpace(10);
            if (!flag)
            {
                bool acess = true;
                foreach (FolderItem file in Recycler.Items())
                {
                    if (File.GetAttributes(file.Path).ToString().Contains((FileAttributes.Directory).ToString()))
                    {
                        base.WalkDir(new DirectoryInfo(file.Path), "system", false, ref size, this.wayFile, ref acess);
                        this.wayDirs.Add(file.Path);
                    }
                    else
                    {
                        this.wayFile.Add(file.Path);
                        size += file.Size;
                    }
                }
            }
            else
            {
                MyDeleteFile.DeleteFiles(this.wayFile);
                foreach (var dir in this.wayDirs)
                {
                    try
                    {
                        Directory.Delete(dir, true);
                    }
                    catch (Exception e)
                    {
                        DirectoryInfo del = new DirectoryInfo(dir);
                        del.Attributes = FileAttributes.Normal;
                        try
                        {
                            del.Delete(true);
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }
            }
            return(string.Format("Recycle files: {0} KB:{1} File(-s)", this.size / 1000, this.wayFile.Count));
        }
예제 #6
0
 /// <summary>
 /// 功能:解压zip格式的文件。
 /// </summary>
 /// <param name="zipFilePath">压缩文件路径</param>
 /// <param name="unZipDir">解压文件存放路径,为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹</param>
 /// <param name="err">出错信息</param>
 /// <returns>解压是否成功</returns>
 public static bool UnZipFile(string zipFilePath, string unZipDir, out string err)
 {
     err = "";
     if (zipFilePath.Length == 0)
     {
         err = "压缩文件不能为空!";
         return(false);
     }
     else if (!zipFilePath.EndsWith(".zip"))
     {
         err = "文件格式不正确!";
         return(false);
     }
     else if (!System.IO.File.Exists(zipFilePath))
     {
         err = "压缩文件不存在!";
         return(false);
     }
     //解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹
     if (unZipDir.Length == 0)
     {
         unZipDir = zipFilePath.Replace(System.IO.Path.GetFileName(zipFilePath), System.IO.Path.GetFileNameWithoutExtension(zipFilePath));
     }
     if (!unZipDir.EndsWith("\\"))
     {
         unZipDir += "\\";
     }
     if (!System.IO.Directory.Exists(unZipDir))
     {
         System.IO.Directory.CreateDirectory(unZipDir);
     }
     try
     {
         Shell32.ShellClass  sc         = new Shell32.ShellClass();
         Shell32.Folder      SrcFolder  = sc.NameSpace(zipFilePath);
         Shell32.Folder      DestFolder = sc.NameSpace(unZipDir);
         Shell32.FolderItems items      = SrcFolder.Items();
         DestFolder.CopyHere(items, 20);
     }
     catch (Exception ex)
     {
         err = ex.Message;
         return(false);
     }
     return(true);
 }//解压结束
예제 #7
0
        //Generates list of songs within the passed directory
        static private void GenerateLib(Shell32.Folder dir, String path)
        {
            foreach (Shell32.FolderItem2 item in dir.Items())
            {
                Console.WriteLine("{0},{1},{2},{3},{4},{5},{6}", dir.GetDetailsOf(item, 0), dir.GetDetailsOf(item, 1), dir.GetDetailsOf(item, 2), dir.GetDetailsOf(item, 10), dir.GetDetailsOf(item, 14), dir.GetDetailsOf(item, 15), dir.GetDetailsOf(item, 16));
                //if there is a folder check inside the folder
                if (dir.GetDetailsOf(item, 2) == "File folder")
                {
                    //Go to the folder
                    String folderPath = path + "\\" + dir.GetDetailsOf(item, 0);
                    Console.WriteLine(folderPath);
                    Shell32.Folder weMustGoDeeper = GetShell32Folder(folderPath);

                    //Generate the list of songs in the folderw
                    GenerateLib(weMustGoDeeper, folderPath);
                }
            }
        }
예제 #8
0
        public static bool unzipWDIFiles(string zipFile, string destinationFolder)
        {
            try
            {
                //Shell32.Folder SrcFlder = GetShell32NameSpaceFolder(from);
                //Shell32.Folder DestFlder = GetShell32NameSpaceFolder(to);
                //FolderItems items = SrcFlder.Items();

                //Shell shell = new Shell();
                Shell32.Folder archive       = GetShell32NameSpaceFolder(zipFile);
                Shell32.Folder extractFolder = GetShell32NameSpaceFolder(destinationFolder);

                // Copy each item one-by-one
                foreach (Shell32.FolderItem f in archive.Items())
                {
                    extractFolder.CopyHere(f, 20);
                }
                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
 private void Setup()
 {
     //select folder
     _objFolder = _shell.NameSpace(_folder);
     //select item
     int i = 0;
     foreach (Shell32.FolderItem2 folderItem2 in _objFolder.Items())
     {
         if (i == _objectnumber)
             _item2 = folderItem2;
         i++;
     }
 }
예제 #10
0
        //http://csharphelper.com/blog/2018/06/get-information-about-windows-shortcuts-in-c/
        // Get information about this link.
        // Return an error message if there's a problem.
        internal static string GetShortcutInfo(string full_name,
                                               out ShortcutItem vSI)
        {
            //name = "";
            //path = "";
            //descr = "";
            //working_dir = "";
            //args = "";
            //var sc = Keys.Control && Keys.LShiftKey && Keys.A;
            //var txt = new KeysConverter().ConvertToString((Keys)sc);
            //Console.WriteLine(txt);



            vSI = new ShortcutItem();

            try
            {
                // Make a Shell object.
                Shell32.Shell shell = new Shell32.Shell();

                // Get the shortcut's folder and name.
                string shortcut_path =
                    full_name.Substring(0, full_name.LastIndexOf("\\"));
                string shortcut_name =
                    full_name.Substring(full_name.LastIndexOf("\\") + 1);
                if (!shortcut_name.EndsWith(".lnk"))
                {
                    shortcut_name += ".lnk";
                }

                // Get the shortcut's folder.
                Shell32.Folder shortcut_folder =
                    shell.NameSpace(shortcut_path);

                // Get the shortcut's file.
                Shell32.FolderItem folder_item =
                    shortcut_folder.Items().Item(shortcut_name);

                if (folder_item == null)
                {
                    return("Cannot find shortcut file '" + full_name + "'");
                }
                if (!folder_item.IsLink)
                {
                    return("File '" + full_name + "' isn't a shortcut.");
                }

                // Display the shortcut's information.
                Shell32.ShellLinkObject lnk = (Shell32.ShellLinkObject)folder_item.GetLink;

                vSI.Arguments   = lnk.Arguments;
                vSI.Description = lnk.Description;
                //vSI.Hotkey = lnk.Hotkey.ToString();
                lnk.GetIconLocation(out string bps);
                vSI.IconPath     = bps;
                vSI.Name         = folder_item.Name;
                vSI.ShortcutPath = lnk.Path;
                FolderItem fi = lnk.Target;
                vSI.Target = GetShortcutTargetFile(full_name);//fi.IsFolder.ToString();
                //vSI.WindowStyle = "1";
                vSI.WorkingDirectory          = lnk.WorkingDirectory;
                vSI.ShortcutPathSpecialFolder = "Other";

                WshShell    theShell    = new WshShell();
                WshShortcut theShortcut = (WshShortcut)theShell.CreateShortcut(full_name);
                vSI.WindowStyle = frmMain.WindowStyleToInt(theShortcut.WindowStyle);
                vSI.Hotkey      = theShortcut.Hotkey.ToString();

                //name = folder_item.Name;
                //descr = lnk.Description;
                //path = lnk.Path;
                //working_dir = lnk.WorkingDirectory;
                //args = lnk.Arguments;

                return("");
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }