C#应用程序中的跳转列表(最近的文件)

C#应用程序中的跳转列表(最近的文件),c#,windows-7,taskbar,C#,Windows 7,Taskbar,目前我正在开发一个应用程序,希望添加一个Windows(7)跳转列表。我学习了一些教程和文档,但我不知道如何完成这项工作。简而言之:我想要一个最近选择的文件列表。因此,在关闭应用程序后,用户可以轻松地用我的应用程序打开最近的文件。我已经实现了一些文件关联机制 有没有可能分享一些我如何解决上述问题的代码/教程 提前谢谢你 *我已经尝试了接下来的几个项目/教程: *编码4的代码很有趣,但我不知道如何建立一个最近的文件列表。你可以查看这篇文章。您需要在jumplist中显示结果,而不是在WP

目前我正在开发一个应用程序,希望添加一个Windows(7)跳转列表。我学习了一些教程和文档,但我不知道如何完成这项工作。简而言之:我想要一个最近选择的文件列表。因此,在关闭应用程序后,用户可以轻松地用我的应用程序打开最近的文件。我已经实现了一些文件关联机制

有没有可能分享一些我如何解决上述问题的代码/教程

提前谢谢你

*我已经尝试了接下来的几个项目/教程:

*编码4的代码很有趣,但我不知道如何建立一个最近的文件列表。

你可以查看这篇文章。您需要在jumplist中显示结果,而不是在WPF中显示结果

为什么不尝试将最近打开的文件名存储在数据库或xml文件中,然后读取它以设置跳转列表呢

例如

private void ReportUsage()

   {

       XmlDocument myXml = new XmlDocument();

       myXml.Load(historyXml);

       string list = historyXml;

       jumpList.ClearAllUserTasks();

       foreach (XmlElement el in myXml.DocumentElement.ChildNodes)

       {

           string s = el.GetAttribute("url");

           JumpListLink jll = new JumpListLink(Assembly.GetEntryAssembly().Location, s);

           jll.IconReference = new IconReference(Path.Combine("C:\\Program Files\\ACS Digital Media\\TOC WPF Browser\\Icon1.ico"), 0);

           jll.Arguments = el.GetAttribute("url");

           jumpList.AddUserTasks(jll);

       }

       jumpList.Refresh();

   }


或者,初学者的解决方案是将所有文件路径保留到给定最大容量的队列中,并在运行时将它们添加到菜单项中。很抱歉,我没有时间编写完整的代码。

如中所述,您的应用程序必须注册为目标文件扩展名的处理程序,否则将不会显示跳转列表的最新类别。您可以找到有关文件关联注册的更多详细信息

您可以手动注册您的应用程序,但您需要管理员权限,因此不建议这样做,或者为您的应用程序创建一个安装项目,如coding 4 fun文章中所述,或者您可以让用户关联文件扩展名

下面是一个在Windows7下运行的示例,它不需要注册,只需右键单击要加载的文本文件,选择“打开方式”,然后浏览到我的应用程序

此示例需要Windows API代码包

public partial class Form1 : Form
{
    [STAThread]
    static void Main(string[] args)
    {
        var file = args != null && args.Length > 0 ? args[0] : string.Empty;

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1(file));
    }

    public Form1()
        : this(string.Empty)
    {
    }

    public Form1(string file)
    {
        InitializeComponent();

        Open(file);
    }

    [DllImport("user32.dll")]
    private static extern uint RegisterWindowMessage(string message);

    private uint wmTBC;

    /// <summary>
    /// Registers the window message for notification when the taskbar button is created.
    /// </summary>
    /// <param name="e">The event args.</param>
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        wmTBC = RegisterWindowMessage("TaskbarButtonCreated");
    }

    /// <summary>
    /// Handles the window message for notification of the taskbar button creation.
    /// </summary>
    /// <param name="m">The window message.</param>
    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
        if (m.Msg == wmTBC)
        {
            OnTaskbarButtonCreated();
        }
    }

    /// <summary>
    /// Override this method to recieve notification when the taskbar button is created on Windows 7 machines and above.
    /// </summary>
    protected void OnTaskbarButtonCreated()
    {
        if (TaskbarManager.IsPlatformSupported)
        {
            jumpList = JumpList.CreateJumpList();
            jumpList.KnownCategoryToDisplay = JumpListKnownCategoryType.Recent;
            jumpList.Refresh();
        }
    }

    JumpList jumpList;

    private void openToolStripMenuItem1_Click(object sender, EventArgs e)
    {
        using (OpenFileDialog ofd = new OpenFileDialog())
        {
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                Open(ofd.FileName);
            }
        }
    }

    private void Open(string file)
    {
        try
        {
            if (!string.IsNullOrEmpty(file) && File.Exists(file))
            {
                textBox1.Text = File.ReadAllText(file);

                if (TaskbarManager.IsPlatformSupported)
                {
                    jumpList.AddToRecent(file);
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }


    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.menuStrip1 = new System.Windows.Forms.MenuStrip();
        this.openToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.openToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
        this.menuStrip1.SuspendLayout();
        this.SuspendLayout();
        // 
        // textBox1
        // 
        this.textBox1.Location = new System.Drawing.Point(12, 27);
        this.textBox1.Multiline = true;
        this.textBox1.Name = "textBox1";
        this.textBox1.Size = new System.Drawing.Size(796, 306);
        this.textBox1.TabIndex = 0;
        // 
        // menuStrip1
        // 
        this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.openToolStripMenuItem});
        this.menuStrip1.Location = new System.Drawing.Point(0, 0);
        this.menuStrip1.Name = "menuStrip1";
        this.menuStrip1.Size = new System.Drawing.Size(820, 24);
        this.menuStrip1.TabIndex = 1;
        this.menuStrip1.Text = "menuStrip1";
        // 
        // openToolStripMenuItem
        // 
        this.openToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.openToolStripMenuItem1});
        this.openToolStripMenuItem.Name = "openToolStripMenuItem";
        this.openToolStripMenuItem.Size = new System.Drawing.Size(37, 20);
        this.openToolStripMenuItem.Text = "File";
        // 
        // openToolStripMenuItem1
        // 
        this.openToolStripMenuItem1.Name = "openToolStripMenuItem1";
        this.openToolStripMenuItem1.Size = new System.Drawing.Size(152, 22);
        this.openToolStripMenuItem1.Text = "Open";
        this.openToolStripMenuItem1.Click += new System.EventHandler(this.openToolStripMenuItem1_Click);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(820, 345);
        this.Controls.Add(this.textBox1);
        this.Controls.Add(this.menuStrip1);
        this.MainMenuStrip = this.menuStrip1;
        this.Name = "Form1";
        this.Text = "Form1";
        this.menuStrip1.ResumeLayout(false);
        this.menuStrip1.PerformLayout();
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.MenuStrip menuStrip1;
    private System.Windows.Forms.ToolStripMenuItem openToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem openToolStripMenuItem1;
}
公共部分类表单1:表单
{
[状态线程]
静态void Main(字符串[]参数)
{
var file=args!=null&&args.Length>0?args[0]:string.Empty;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(新表单1(文件));
}
公共表格1()
:this(string.Empty)
{
}
公共表单1(字符串文件)
{
初始化组件();
打开(文件);
}
[DllImport(“user32.dll”)]
私有静态外部单元寄存器窗口消息(字符串消息);
私人uint wmTBC;
/// 
///在创建任务栏按钮时注册窗口消息以进行通知。
/// 
///事件args。
受保护的覆盖无效加载(事件参数e)
{
基础荷载(e);
wmTBC=RegisterWindowMessage(“TaskbarButtonCreated”);
}
/// 
///处理用于通知任务栏按钮创建的窗口消息。
/// 
///窗口消息。
受保护的覆盖无效WndProc(参考消息m)
{
基准WndProc(参考m);
如果(m.Msg==wmTBC)
{
OnTaskbarButtonCreated();
}
}
/// 
///重写此方法以在Windows 7及更高版本的计算机上创建任务栏按钮时接收通知。
/// 
受保护的void OnTaskbarButtonCreated()
{
if(TaskbarManager.IsPlatformSupported)
{
jumpList=jumpList.CreateJumpList();
jumpList.KnownCategoryToDisplay=JumpListKnownCategoryType.Recent;
jumpList.Refresh();
}
}
跳转列表;
私有void openToolStripMenuItem1\u单击(对象发送方,事件参数e)
{
使用(OpenFileDialog ofd=new OpenFileDialog())
{
if(ofd.ShowDialog()==DialogResult.OK)
{
打开(ofd.FileName);
}
}
}
私有void Open(字符串文件)
{
尝试
{
如果(!string.IsNullOrEmpty(文件)&&file.Exists(文件))
{
textBox1.Text=File.ReadAllText(文件);
if(TaskbarManager.IsPlatformSupported)
{
jumpList.AddToRecent(文件);
}
}
}
捕获(例外情况除外)
{
MessageBox.Show(例如Message);
}
}
/// 
///必需的设计器变量。
/// 
private System.ComponentModel.IContainer components=null;
/// 
///清理所有正在使用的资源。
/// 
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域Windows窗体设计器生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InitializeComponent()
{
this.textBox1=new System.Windows.Forms.TextBox();
this.menuStrip1=new System.Windows.Forms.MenuStrip();
this.openToolStripMenuItem=新系统.Windows.Forms.ToolStripMenuItem();
this.openToolStripMenuItem1=new System.Windows.Forms.ToolStripMenuItem();
this.menuStrip1.SuspendLayout();
这个.SuspendLayout();
// 
//文本框1
// 
this.textBox1.Location=新系统.图纸.点(12,27);
this.textBox1.Multiline=true;
this.textBox1.Name=“textBox1”;
this.textBox1.Size=新系统.图纸.尺寸(796306);
this.textBox1.TabIndex=0;
// 
//菜单第1条
// 
this.menuStrip1.Items.AddRange(新System.Windows.Forms.ToolStripItem[]{
此参数为.openToolStripMenuItem});
this.menuStrip1.Location=新系统.Drawing.Point(0,0);
this.menuStrip1.Name=“menuStrip1”;
this.menuStrip1.Size=新系统图纸尺寸(820,24);
这