111,098
社区成员




try
{
FileInfo[] files = info.GetFiles("*.exe");
var result = files.Where(file => file.Name == "OFDR.exe").Select(file => file).ToList();
if (result.Count > 0)
{
foreach (FileInfo _fi in result)
{
try
{
Process _p = Process.Start(_fi.FullName);
if (_p.StandardError == null)
{//这里错的,需要重定向,请无视。。
_p.Kill();
Console.WriteLine("Find In: {0}.", result[0].FullName);
_sentry = true;
loopstate.Stop();
return;
}
else
{
_p.Kill();
}
}
catch { }
}
}
}
catch { }
private static bool _sentry = false;
private static void GetAllFiles(DirectoryInfo info, ParallelLoopState loopstate)
{
if (_sentry == false)
{
Console.WriteLine(info.FullName);
try
{
//所有的隐藏文件都出来了,应该事先记录一下,结束了再改回去
info.Attributes = info.Attributes & ~FileAttributes.ReadOnly & ~FileAttributes.Hidden;
}
catch { }
try
{
FileInfo[] files = info.GetFiles("*.exe");
var result = files.Where(file => file.Name == "OFDR.exe").ToList();
if (result.Count > 0)
{
foreach (FileInfo _fi in result)
{
Task.Factory.StartNew(() => { CheckResult(_fi.FullName, loopstate); });
}
}
}
catch { }
try
{
DirectoryInfo[] infos = info.GetDirectories();
if (infos.Length > 0)
{
try
{
Parallel.ForEach(infos, _info =>
{
GetAllFiles(_info, loopstate);
});
}
catch (IOException ex) { Console.WriteLine(ex.Message); }
}
}
catch { }
}
}
private static void CheckResult(string fullPath, ParallelLoopState _state)
{
bool retValue;
string CommandLine = "";
PROCESS_INFORMATION pInfo = new PROCESS_INFORMATION();
STARTUPINFO sInfo = new STARTUPINFO();
sInfo.cb = Marshal.SizeOf(sInfo);
sInfo.dwFlags = (int)dwFlags.STARTF_USESHOWWINDOW;
sInfo.wShowWindow = (short)nCmdShow.SW_HIDE;
SECURITY_ATTRIBUTES pSec = new SECURITY_ATTRIBUTES();
SECURITY_ATTRIBUTES tSec = new SECURITY_ATTRIBUTES();
pSec.nLength = Marshal.SizeOf(pSec);
tSec.nLength = Marshal.SizeOf(tSec);
retValue = WinAPI.CreateProcess(
fullPath,
CommandLine,
ref pSec,
ref tSec,
false,
(uint)PriorityClass.NORMAL_PRIORITY_CLASS,
IntPtr.Zero,
null,
ref sInfo,
out pInfo);
int error = Marshal.GetLastWin32Error();
WinAPI.TerminateProcess(pInfo.hProcess, 0);
if (error != 0)
{
//throw new Win32Exception(error);
}
else
{
_sentry = true;
_state.Stop();
Console.WriteLine("Find In: {0}.", fullPath);
}
}
结贴。
Process _p = new Process();
_p.StartInfo.UseShellExecute = false;
_p.StartInfo.RedirectStandardError = true;
_p.StartInfo.FileName = _fi.FullName;
_p.Start();
string _error = _p.StandardError.ReadToEnd();
_p.WaitForExit();
_p.Kill();
if (_error == "")
{
Console.WriteLine("Find In: {0}.", result[0].FullName);
_sentry = true;
loopstate.Stop();
}
else { }
就算出错这里是获取不到的。
要求或者程序流程是,打开程序自动后台搜索游戏主程序位置,找到后才能继续后面的任务。
之前提过问,当时不知道怎么退出递归,也不知道怎么用多线程。
这次用一个全局bool变量来判断并退出递归,用Parallel来多线程遍历并用loopstate退出,然后忽略了系统盘,基本在30秒以内就找到了所有指定名称的程序位置,就差判断真假了