Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用C将lnk文件固定到win 7任务栏#_C# - Fatal编程技术网

C# 使用C将lnk文件固定到win 7任务栏#

C# 使用C将lnk文件固定到win 7任务栏#,c#,C#,即使是Win7中图标的编程固定似乎也是不允许的(就像这里说的:),有些VB脚本也有这样做的方法。有人在C#中找到了这样一种方法: private static void PinUnpinTaskBar(string filePath, bool pin) { if (!File.Exists(filePath)) throw new FileNotFoundException(filePath); // create the shell application object dynami

即使是Win7中图标的编程固定似乎也是不允许的(就像这里说的:),有些VB脚本也有这样做的方法。有人在C#中找到了这样一种方法:

private static void PinUnpinTaskBar(string filePath, bool pin)
{
 if (!File.Exists(filePath)) throw new FileNotFoundException(filePath);

 // create the shell application object
 dynamic shellApplication = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"));

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

 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);
        string verbName = verb.Name.Replace(@"&", string.Empty).ToLower();

        if ((pin && verbName.Equals("pin to taskbar")) || (!pin && verbName.Equals("unpin from taskbar")))
        {

            verb.DoIt();
        }
    }

    shellApplication = null;
}
private static void PinUnpinTaskBar(字符串文件路径,bool pin)
{
如果(!File.Exists(filePath))抛出新的FileNotFoundException(filePath);
//创建shell应用程序对象
dynamic shellApplication=Activator.CreateInstance(Type.GetTypeFromProgID(“Shell.Application”));
string path=path.GetDirectoryName(filePath);
字符串fileName=Path.GetFileName(filePath);
动态目录=shellApplication.NameSpace(路径);
动态链接=directory.ParseName(文件名);
动态动词=link.verbs();
for(int i=0;i
可以看出,该代码利用了.NETFramework 4.0的功能。我想问的问题是:这个函数可以被转换成同样的东西,但只使用3.5框架吗?有什么想法吗?
谢谢大家!

我删除了
动态
的使用,并将其替换为反射调用。它很难看,除了确保它在.NET3.5下编译之外,我没有对它进行测试,但是试一试。您需要使用System.Reflection添加
到你的类,如果你还没有它

private static void PinUnpinTaskBar(string filePath, bool pin)
{
    if (!File.Exists(filePath)) throw new FileNotFoundException(filePath);

    // create the shell application object
    var shellType = Type.GetTypeFromProgID("Shell.Application");

    var shellApplication = Activator.CreateInstance(shellType);

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

    var directory = shellType.InvokeMember("Namespace", BindingFlags.InvokeMethod, null, shellApplication, new object[] { path });
    var link = directory.GetType().InvokeMember("ParseName", BindingFlags.InvokeMethod, null, directory, new object[] {fileName});
    var verbs = link.GetType().InvokeMember("Verbs", BindingFlags.InvokeMethod, null, link, new object[] { });

    int verbsCount = (int)verbs.GetType().InvokeMember("Count", BindingFlags.InvokeMethod, null, verbs, new object[] { });

    for (int i = 0; i < verbsCount; i++)
    {
        var verb = verbs.GetType().InvokeMember("Item", BindingFlags.InvokeMethod, null, verbs, new object[] { i });

        var namePropertyValue = (string)verb.GetType().GetProperty("Name").GetValue(verb, null);
        var verbName = namePropertyValue.Replace(@"&", string.Empty).ToLower();

        if ((pin && verbName.Equals("pin to taskbar")) || (!pin && verbName.Equals("unpin from taskbar")))
        {

            verbs.GetType().InvokeMember("DoIt", BindingFlags.InvokeMethod, null, verbs, new object[] { });
        }
    }

    shellApplication = null;
}
private static void PinUnpinTaskBar(字符串文件路径,bool pin)
{
如果(!File.Exists(filePath))抛出新的FileNotFoundException(filePath);
//创建shell应用程序对象
var shellType=Type.GetTypeFromProgID(“Shell.Application”);
var shellApplication=Activator.CreateInstance(shellType);
string path=path.GetDirectoryName(filePath);
字符串fileName=Path.GetFileName(filePath);
var directory=shellType.InvokeMember(“名称空间”,BindingFlags.InvokeMethod,null,shellApplication,新对象[]{path});
var link=directory.GetType().InvokeMember(“ParseName”,BindingFlags.InvokeMethod,null,directory,新对象[]{fileName});
var verbs=link.GetType().InvokeMember(“动词”,BindingFlags.InvokeMethod,null,link,新对象[]{});
int verbscont=(int)verbs.GetType().InvokeMember(“Count”,BindingFlags.InvokeMethod,null,verbs,新对象[]{});
for(int i=0;i
用户已经有办法将程序固定到任务栏上。为什么需要发明另一种方法?这会引发ComException,错误代码为DISP_E_MEMBERNOTFOUND