📄 form1.h
字号:
#pragma once
namespace Example {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Diagnostics;
using namespace System::IO;
/// <summary>
/// Form1 摘要
///
/// 警告: 如果更改此类的名称,则需要更改
/// 与此类所依赖的所有 .resx 文件关联的托管资源编译器工具的
/// “资源文件名”属性。否则,
/// 设计器将不能与此窗体的关联
/// 本地化资源正确交互。
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: 在此处添加构造函数代码
//
}
protected:
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Label^ label1;
protected:
private: System::Windows::Forms::TextBox^ textBox1;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::CheckBox^ checkBox1;
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::RichTextBox^ richTextBox1;
private:
/// <summary>
/// 必需的设计器变量。
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
void InitializeComponent(void)
{
this->label1 = (gcnew System::Windows::Forms::Label());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->label2 = (gcnew System::Windows::Forms::Label());
this->checkBox1 = (gcnew System::Windows::Forms::CheckBox());
this->button1 = (gcnew System::Windows::Forms::Button());
this->richTextBox1 = (gcnew System::Windows::Forms::RichTextBox());
this->SuspendLayout();
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(4, 8);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(77, 12);
this->label1->TabIndex = 0;
this->label1->Text = L"查找文件名:";
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(79, 3);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(306, 21);
this->textBox1->TabIndex = 1;
this->textBox1->Text = L"Notepad.exe";
//
// label2
//
this->label2->AutoSize = true;
this->label2->Location = System::Drawing::Point(4, 32);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(113, 12);
this->label2->TabIndex = 2;
this->label2->Text = L"当前文件查找结果:";
//
// checkBox1
//
this->checkBox1->AutoSize = true;
this->checkBox1->Location = System::Drawing::Point(162, 32);
this->checkBox1->Name = L"checkBox1";
this->checkBox1->Size = System::Drawing::Size(96, 16);
this->checkBox1->TabIndex = 3;
this->checkBox1->Text = L"是否只找一个";
this->checkBox1->UseVisualStyleBackColor = true;
//
// button1
//
this->button1->Location = System::Drawing::Point(310, 26);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 4;
this->button1->Text = L"开始查找";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// richTextBox1
//
this->richTextBox1->Location = System::Drawing::Point(6, 50);
this->richTextBox1->Name = L"richTextBox1";
this->richTextBox1->Size = System::Drawing::Size(379, 156);
this->richTextBox1->TabIndex = 5;
this->richTextBox1->Text = L"";
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(390, 210);
this->Controls->Add(this->richTextBox1);
this->Controls->Add(this->button1);
this->Controls->Add(this->checkBox1);
this->Controls->Add(this->label2);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->label1);
this->Name = L"Form1";
this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
this->Text = L"演示在指定路径中搜索文件";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
//新添加成员
private: static ArrayList^ MyFindPath = gcnew ArrayList();
private: static ArrayList^ MyExtsList = gcnew ArrayList();
private: static array<String^>^ MyDefaultExtensions ={".COM",".PDB", ".EXE", ".BAT",
".CMD", ".VBS", ".CS",".VBE", ".JS", ".JSE", ".WSF",".WSH"};
private: static array<wchar_t>^ MySemicolon = {';'};
private: static int MyExtsCount = 0;
private: static int MyDirsCount = 0;
private: static bool MyFirstMatchOnly = false;
//添加路径数据
public:static void addElementToList(String^ element, ArrayList^ arrayList)
{
for (int i=0; i<arrayList->Count; i++)
{
if (0 == String::Compare(element, (String^)arrayList[i], true))
return;
}
arrayList->Add(element);
}
//添加指定路径数据
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
//添加本程序文件所在目录.
System::Diagnostics::Process^ MyCurrentProcess = System::Diagnostics::Process::GetCurrentProcess();
ProcessModule^ MyProcessModule = MyCurrentProcess->MainModule;
String^ MyDir = Path::GetDirectoryName(MyProcessModule->FileName);
if ((MyDir->Length > 1) && ("\\"==System::Convert::ToString(MyDir[MyDir->Length-1])))
MyDir->Remove(MyDir->Length-1,1);
MyFindPath->Add(MyDir);
//添加当前目录和系统目录
String^ MyCurrentDir = Environment::CurrentDirectory;
if ((MyCurrentDir->Length > 1)&&("\\" ==System::Convert::ToString(MyCurrentDir[MyCurrentDir->Length-1])))
MyCurrentDir->Remove(MyCurrentDir->Length-1,1);
addElementToList(MyCurrentDir, MyFindPath);
String^ MySystemDir = Environment::SystemDirectory;
if ((MySystemDir->Length > 1 )&& ("\\" ==System::Convert::ToString(MySystemDir[MySystemDir->Length-1])))
MySystemDir->Remove(MySystemDir->Length-1,1);
addElementToList(MySystemDir, MyFindPath);
//添加Windows目录
String^ MyWindowsDir = Directory::GetParent(MySystemDir)->FullName;
if ((MyWindowsDir->Length > 1)&& ("\\" == System::Convert::ToString(MyWindowsDir[MyWindowsDir->Length-1])))
MyWindowsDir->Remove(MyWindowsDir->Length-1,1);
addElementToList(MyWindowsDir, MyFindPath);
//添加环境变量所指目录
String^ MyPathString = Environment::GetEnvironmentVariable("Path");
if (MyPathString != String::Empty)
{
array<String^>^ MyPathElements=MyPathString->Split(MySemicolon);
int MyPathElementCount = MyPathElements->GetLength(0);
for (int i=0; i<MyPathElementCount; i++)
{
String^ MyElement =System::Convert::ToString(MyPathElements[i]);
if ((MyElement->Length > 1) &&("\\" ==System::Convert::ToString(MyElement[MyElement->Length-1])))
MyElement->Remove(MyElement->Length-1,1);
addElementToList(MyElement, MyFindPath);
}
}
MyDirsCount = MyFindPath->Count;
// 创建文件扩展名列表
array<String^>^ MyExtensionArray ;
String^ MyPathExt = Environment::GetEnvironmentVariable("PathExt");
if (String::Empty == MyPathExt)
MyDefaultExtensions->CopyTo(MyExtensionArray,0);
else
MyExtensionArray = MyPathExt->Split(MySemicolon);
MyExtsCount = MyExtensionArray->Length;
for (int i=0; i<MyExtsCount; i++)
addElementToList(MyExtensionArray[i], MyExtsList);
}
//开始查找
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
this->richTextBox1->Text="";
this->MyFirstMatchOnly=this->checkBox1->Checked;
if(this->textBox1->Text==String::Empty)
return;
for (int i=0; i<MyDirsCount; i++)
{
bool MyExists = false;
String^ MyExtStr= Path::GetExtension(this->textBox1->Text);
if (MyExtStr != String::Empty)
{
String^ MyFile = String::Format("{0}\\{1}", (String^)MyFindPath[i],this->textBox1->Text);
if ((MyExists = File::Exists(MyFile)))
//if ((MyExists == File::Exists(MyFile)))
this->richTextBox1->AppendText(MyFile+"\n");
}
else
{
for (int j=0; j<MyExtsCount; j++)
{
String^ MyFile = String::Format("{0}\\{1}{2}",(String^)MyFindPath[i],this->textBox1->Text, MyExtsList[j]);
if ((MyExists = File::Exists(MyFile)))
// if ((MyExists == File::Exists(MyFile)))
{
this->richTextBox1->AppendText(MyFile+"\n");
if (MyFirstMatchOnly)
break;
}
}
}
if (MyExists && MyFirstMatchOnly)
break;
}
}
};
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -