如何用Process类打开便笺?

时间:2014-06-01 21:21:16

标签: c#

我想用流程类打开windows便签,我已经知道如何为计算器和绘画做这个,但不是粘滞便笺。当我给它粘滞便笺地址时,它不会打开。我使用的是64位窗口。 (窗体)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace main
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("mspaint");
            System.Diagnostics.Process.Start("calc");
            //sticky notes
            System.Diagnostics.Process.Start("C:\\Windows\\System32\\StikyNot.exe");
        }
    }
}

它给出了无法找到文件的异常,但它存在于该位置。

3 个答案:

答案 0 :(得分:3)

您必须从C:\Windows\Sysnative\StikyNot.exe打开文件:

System.Diagnostics.Process.Start(@"C:\Windows\Sysnative\StikyNot.exe");

由于x64系统上的System32文件夹已合成,因此直接调用它不起作用。您必须使用Sysnative名称将路径映射到“真正的”system32文件夹。

答案 1 :(得分:1)

此位置适用于我赢得8.1 64位

System.Diagnostics.Process.Start(@"C:\Windows\WinSxS\amd64_microsoft-windows-stickynotes-app_31bf3856ad364e35_6.3.9600.16384_none_de0517088d429664\StikyNot.exe");

如果它不适合您,您可以在Windows驱动器中搜索StikyNot的其他位置

答案 2 :(得分:0)

如果您想进一步玩游戏,可以使用以下程序作为参考。这似乎是一个非常好的。

这是一个示例调用。实现逻辑如下。

public void NewNote(string someText)
{
var note = new StickyNote();
note.Activate();
note.Activated += (s, e) => { note.Signal(someText); };
}

信用转到Jesper Neidermann's Blog

using System;  
using System.Diagnostics;  
using System.IO;  
using System.Runtime.InteropServices;  
using System.Threading;  
using System.Windows.Forms;  

namespace DayView  
{  
    public class StickyNote  
    {  
        private const string m_ProcessName = "StikyNot";  
        private readonly string m_ProcessFileName = Path.Combine(Environment.SystemDirectory, "StikyNot.exe");  
        private event EventHandler m_Activated = delegate { };  
        [DllImport("user32.dll")]  
        [return: MarshalAs(UnmanagedType.Bool)]  
        static extern bool SetForegroundWindow(IntPtr hWnd);  

        public void Activate()  
        {  
            bool makeNewNote = true;  
            Process p = FindProcess();  
            if (p == null)  
            {  
                p = StartProcess();  
                if (!NoteContainsText(p.MainWindowHandle))  
                {  
                    makeNewNote = false;  
                }  
            }  
            var state = new StickyNoteState();  
            state.MakeNewNote = makeNewNote;  
            state.StickyNoteProcess = p;  
            ThreadPool.QueueUserWorkItem(Activate, state);  
        }  

        private void Activate(object state)  
        {  
            var stickyNoteState = state as StickyNoteState;  
            if (stickyNoteState.MakeNewNote)  
            {  
                NewNote(stickyNoteState.StickyNoteProcess);  
            }  
            OnActivated();  
        }  

        private Process StartProcess()  
        {  
            var startInfo = new ProcessStartInfo(m_ProcessFileName);  
            Process p = Process.Start(startInfo);  
            Thread.Sleep(200); //This is an annoying hack. I haven't been able to find another way to be sure the process is started.  
            return p;  
        }  

        private void NewNote(Process p)  
        {  
            SetForegroundWindow(p.MainWindowHandle);  
            Signal("^n");              
        }  

        /// <summary>  
        /// Weird hack to find out if note contains text.  
        /// </summary>  
        /// <returns></returns>  
        private bool NoteContainsText(IntPtr handle)  
        {  
            string textOfClipboard = Clipboard.GetText();  
            Signal("^a");  
            Signal("^c");  
            Signal("{RIGHT}");  
            string noteText = Clipboard.GetText().Trim();  
            if (textOfClipboard == null)  
            {  
                Clipboard.SetText(textOfClipboard);  
            }  
            return !string.IsNullOrEmpty(noteText);  
        }  

        private Process FindProcess()  
        {  
                Process[] processes = Process.GetProcessesByName(m_ProcessName);  
                if(processes != null && processes.Length > 0)  
                {  
                    return processes[0];  
                }  
            return null;  
        }  

        internal void OnActivated()  
        {  
            m_Activated(this, new EventArgs());  
        }  

        public event EventHandler Activated  
        {  
            add { m_Activated += value; }  
            remove { m_Activated -= value; }  
        }  

        public void Signal(string message)  
        {  
            SendKeys.SendWait(message);  
            SendKeys.Flush();  
        }  
    }  

    public class StickyNoteState  
    {  
        public bool MakeNewNote { get; set; }  
        public Process StickyNoteProcess { get; set; }  

    }  
}  

您还需要添加应用设置条目。

<?xml version="1.0" encoding="utf-8" ?>  
<configuration>  
  <appSettings>  
    <add key="SendKeys" value="SendInput"/>  
  </appSettings>  
</configuration>