主题:用c#调用notepad时路径问题,请高手指点一下
小弟写了个守护notepad进程的小程序,每两秒钟扫描一下进程,如果没有notepad进程,则运行notepad,代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.ComponentModel;
using System.Timers;
namespace myprocess
{
public class GetMyProcess
{
void DigitalChinawebaClient()
{
Process[] notepad = Process.GetProcesses();
int i = 0;
foreach (Process notepad1 in notepad)
{
if (notepad1.ProcessName != "notepad")
{
i++;
if (i == notepad.Length)
{
[color=FF0000]string patch = @"notepad.exe";[/color]
Process thepatch = new Process();
thepatch.StartInfo.FileName = patch;
thepatch.Start();
continue;
}
}
}
}
static void Main()
{
Timer aTimer = new Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 2000;
aTimer.Enabled = true;
Console.WriteLine("正在监视notepad进程");
Console.ReadLine();
GC.KeepAlive(aTimer);
}
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
GetMyProcess digitalChinawebaClient = new GetMyProcess();
digitalChinawebaClient.DigitalChinawebaClient();
}
}
}
以上代码用Microsoft Visual Studio 2005编译后运行正常,
但是请注意红色部分,如果红色部分改成string patch = @"c:\windows\notepad.exe";
则会不停地运行notepad,小弟十分不解,为什么仅仅把路径改成绝对路径,运行起来就不一样了呢,请高手指点一二,万分感谢!
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.ComponentModel;
using System.Timers;
namespace myprocess
{
public class GetMyProcess
{
void DigitalChinawebaClient()
{
Process[] notepad = Process.GetProcesses();
int i = 0;
foreach (Process notepad1 in notepad)
{
if (notepad1.ProcessName != "notepad")
{
i++;
if (i == notepad.Length)
{
[color=FF0000]string patch = @"notepad.exe";[/color]
Process thepatch = new Process();
thepatch.StartInfo.FileName = patch;
thepatch.Start();
continue;
}
}
}
}
static void Main()
{
Timer aTimer = new Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 2000;
aTimer.Enabled = true;
Console.WriteLine("正在监视notepad进程");
Console.ReadLine();
GC.KeepAlive(aTimer);
}
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
GetMyProcess digitalChinawebaClient = new GetMyProcess();
digitalChinawebaClient.DigitalChinawebaClient();
}
}
}
以上代码用Microsoft Visual Studio 2005编译后运行正常,
但是请注意红色部分,如果红色部分改成string patch = @"c:\windows\notepad.exe";
则会不停地运行notepad,小弟十分不解,为什么仅仅把路径改成绝对路径,运行起来就不一样了呢,请高手指点一二,万分感谢!