使用C#引导到任务栏

发布于 2025-02-03 03:30:56 字数 1681 浏览 2 评论 0原文

我搜索了同一主题,并找到了解决方案。但是问题是名称空间属性给出了例外。我已经尝试了此代码:

    public bool PinUnpinTaskbar(string filePath, bool pin)
    {
       
       if (!File.Exists(filePath)) throw new FileNotFoundException(filePath);
        int MAX_PATH = 255;
        var actionIndex = pin ? 5386 : 5387; // 5386 is the DLL index for"Pin to Tas&kbar", ref. http://www.win7dll.info/shell32_dll.html
                                             //uncomment the following line to pin to start instead
                                             //actionIndex = pin ? 51201 : 51394;
        StringBuilder szPinToStartLocalized = new StringBuilder(MAX_PATH);
        IntPtr hShell32 = LoadLibrary("Shell32.dll");
        LoadString(hShell32, (uint)actionIndex, szPinToStartLocalized, MAX_PATH);
        
        string localizedVerb = szPinToStartLocalized.ToString();

        string path = Path.GetDirectoryName(filePath);
        string fileName = Path.GetFileName(filePath);

        // create the shell application object
        //var shell = new Shell32.Shell();
        dynamic shellApplication = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"));
        dynamic directory = shellApplication.NameSpace(path);
        dynamic link = directory.ParseName(fileName);

        dynamic verbs = link.Verbs();
        for (int i = 0; i < verbs.Count(); i++)
        {
            dynamic verb = verbs.Item(i);
            if (verb.Name.Equals(localizedVerb))
            {
                verb.DoIt();
                return true;
            }
        }
        return false;
    }

但是问题在于ShellApplication.namespace(路径)。它找不到这样的属性/方法。我对此没有太多的想法。因此,将不胜感激。

I have searched for this same topic and I found the solution. But the problem is the NameSpace property is giving exception. I have tried with this code :

    public bool PinUnpinTaskbar(string filePath, bool pin)
    {
       
       if (!File.Exists(filePath)) throw new FileNotFoundException(filePath);
        int MAX_PATH = 255;
        var actionIndex = pin ? 5386 : 5387; // 5386 is the DLL index for"Pin to Tas&kbar", ref. http://www.win7dll.info/shell32_dll.html
                                             //uncomment the following line to pin to start instead
                                             //actionIndex = pin ? 51201 : 51394;
        StringBuilder szPinToStartLocalized = new StringBuilder(MAX_PATH);
        IntPtr hShell32 = LoadLibrary("Shell32.dll");
        LoadString(hShell32, (uint)actionIndex, szPinToStartLocalized, MAX_PATH);
        
        string localizedVerb = szPinToStartLocalized.ToString();

        string path = Path.GetDirectoryName(filePath);
        string fileName = Path.GetFileName(filePath);

        // create the shell application object
        //var shell = new Shell32.Shell();
        dynamic shellApplication = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"));
        dynamic directory = shellApplication.NameSpace(path);
        dynamic link = directory.ParseName(fileName);

        dynamic verbs = link.Verbs();
        for (int i = 0; i < verbs.Count(); i++)
        {
            dynamic verb = verbs.Item(i);
            if (verb.Name.Equals(localizedVerb))
            {
                verb.DoIt();
                return true;
            }
        }
        return false;
    }

But the problem is with shellApplication.Namespace(path) here. It can not find such property/Method. I don't have too much idea on this. So, any kind of help will be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

演多会厌 2025-02-10 03:30:56

WRT W10:我唯一能够对此有意义的方法是;

由用户决定“销钉到任务栏”。但是,您必须设置最终发生的参考。也就是说,如果您希望您的应用程序组合应用程序和快速启动图标。否则,每次程序启动时,都会在任务栏中创建第二个图标。

public void MakeAppPinnable()
{
    var TaskbarPath =
        Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) +
        "\\AppData\\Roaming\\Microsoft\\Internet Explorer\\Quick Launch";

    CreateShortcut(
        System.IO.Path.Combine(TaskbarPath, <AppName>+ ".lnk"),
        System.Reflection.Assembly.GetExecutingAssembly().Location
        );
 }

 private void CreateShortcut(string LnkPath, string ExePath)
 {
     if (System.IO.File.Exists(LnkPath)) {return;}

     var shell = new IWshRuntimeLibrary.WshShell();
     var shortcut = (IWshRuntimeLibrary.IWshShortcut)(shell.CreateShortcut(LnkPath));
     shortcut.TargetPath = shortcut.IconLocation = ExePath;
     shortcut.Save();
  }

当用户手动将应用程序固定到任务栏时,将在快速启动文件夹中创建第二个快捷键。但是,这个降落在... \ Quick启动\用户固定\任务栏中。不要问我为什么这一切都需要在... \ roaming \ Microsoft \ Internet Explorer ...疯狂!

干杯。

WRT W10: The only way I have ever been able to make any sense of this is;

It's up to user to "pin to taskbar". However, you must setup a reference for when it eventually happens. That is, if you want your app to combine the app and quick launch icons. Otherwise, a second icon will be created in the taskbar every time the program starts.

public void MakeAppPinnable()
{
    var TaskbarPath =
        Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) +
        "\\AppData\\Roaming\\Microsoft\\Internet Explorer\\Quick Launch";

    CreateShortcut(
        System.IO.Path.Combine(TaskbarPath, <AppName>+ ".lnk"),
        System.Reflection.Assembly.GetExecutingAssembly().Location
        );
 }

 private void CreateShortcut(string LnkPath, string ExePath)
 {
     if (System.IO.File.Exists(LnkPath)) {return;}

     var shell = new IWshRuntimeLibrary.WshShell();
     var shortcut = (IWshRuntimeLibrary.IWshShortcut)(shell.CreateShortcut(LnkPath));
     shortcut.TargetPath = shortcut.IconLocation = ExePath;
     shortcut.Save();
  }

When the user manually pins app to taskbar, a second shortcut is created in the quick launch folder. However, this one lands in ...\Quick Launch\User Pinned\TaskBar. Do not ask me why this all needs to happen in ...\Roaming\Microsoft\Internet Explorer... MADNESS!

Cheers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文