求助 : CLR 中文件重命名
求助 : FileSystem::Rename 无法使用!!!字符串编辑测试已经通过了。
就是求助 FileSystem::Renam() 无法使用!!!
不知道,是什么原因?????
自己实在没有办法了。。。只能绕路走了。。。
想起了以前C语言中的rename()
于是#include<iostream>
再把String^ 转化成 const char*
这样太麻烦了,好像回到了20年前的编程时代。
FileSystem::Rename 无法使用到底什么原因
程序代码:
#pragma once namespace CLIRENAME { using namespace System; using namespace System::IO; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; using namespace System::Text::RegularExpressions; /// <summary> /// Form1 摘要 /// </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::TextBox^ textBox1; protected: private: System::Windows::Forms::TextBox^ textBox2; private: System::Windows::Forms::TextBox^ textBox3; private: System::Windows::Forms::TextBox^ textBox4; private: System::Windows::Forms::TextBox^ textBox5; private: System::Windows::Forms::Button^ button1; private: System::Windows::Forms::Button^ button2; private: System::Windows::Forms::Button^ button3; private: System::Windows::Forms::Label^ label1; private: System::Windows::Forms::Label^ label2; private: System::Windows::Forms::Label^ label3; private: System::Windows::Forms::Label^ label4; private: System::Windows::Forms::FolderBrowserDialog^ folderBrowserDialog1; private: /// <summary> /// 必需的设计器变量。 /// </summary> System::ComponentModel::Container ^components; #pragma region Windows Form Designer generated code /// <summary> /// 设计器支持所需的方法 - 不要修改 /// 使用代码编辑器修改此方法的内容。 /// </summary> void InitializeComponent(void) { this->textBox1 = (gcnew System::Windows::Forms::TextBox()); this->textBox2 = (gcnew System::Windows::Forms::TextBox()); this->textBox3 = (gcnew System::Windows::Forms::TextBox()); this->textBox4 = (gcnew System::Windows::Forms::TextBox()); this->textBox5 = (gcnew System::Windows::Forms::TextBox()); this->button1 = (gcnew System::Windows::Forms::Button()); this->button2 = (gcnew System::Windows::Forms::Button()); this->button3 = (gcnew System::Windows::Forms::Button()); this->label1 = (gcnew System::Windows::Forms::Label()); this->label2 = (gcnew System::Windows::Forms::Label()); this->label3 = (gcnew System::Windows::Forms::Label()); this->label4 = (gcnew System::Windows::Forms::Label()); this->folderBrowserDialog1 = (gcnew System::Windows::Forms::FolderBrowserDialog()); this->SuspendLayout(); // // textBox1 // this->textBox1->BackColor = System::Drawing::SystemColors::AppWorkspace; this->textBox1->Font = (gcnew System::Drawing::Font(L"宋体", 12)); this->textBox1->Location = System::Drawing::Point(23, 29); this->textBox1->Name = L"textBox1"; this->textBox1->ReadOnly = true; this->textBox1->Size = System::Drawing::Size(808, 26); this->textBox1->TabIndex = 0; // // textBox2 // this->textBox2->Font = (gcnew System::Drawing::Font(L"宋体", 12)); this->textBox2->Location = System::Drawing::Point(23, 81); this->textBox2->Name = L"textBox2"; this->textBox2->Size = System::Drawing::Size(229, 26); this->textBox2->TabIndex = 1; // // textBox3 // this->textBox3->Font = (gcnew System::Drawing::Font(L"宋体", 12)); this->textBox3->Location = System::Drawing::Point(302, 81); this->textBox3->Name = L"textBox3"; this->textBox3->Size = System::Drawing::Size(354, 26); this->textBox3->TabIndex = 2; // // textBox4 // this->textBox4->Font = (gcnew System::Drawing::Font(L"宋体", 12)); this->textBox4->Location = System::Drawing::Point(707, 79); this->textBox4->Name = L"textBox4"; this->textBox4->Size = System::Drawing::Size(229, 26); this->textBox4->TabIndex = 3; // // textBox5 // this->textBox5->BackColor = System::Drawing::SystemColors::Window; this->textBox5->Font = (gcnew System::Drawing::Font(L"宋体", 12)); this->textBox5->Location = System::Drawing::Point(23, 162); this->textBox5->Multiline = true; this->textBox5->Name = L"textBox5"; this->textBox5->ReadOnly = true; this->textBox5->ScrollBars = System::Windows::Forms::ScrollBars::Both; this->textBox5->Size = System::Drawing::Size(915, 365); this->textBox5->TabIndex = 4; // // button1 // this->button1->Location = System::Drawing::Point(858, 27); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(78, 30); this->button1->TabIndex = 5; this->button1->Text = L"浏 览"; this->button1->UseVisualStyleBackColor = true; this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click); // // button2 // this->button2->BackColor = System::Drawing::Color::Cyan; this->button2->Font = (gcnew System::Drawing::Font(L"宋体", 14)); this->button2->Location = System::Drawing::Point(116, 117); this->button2->Name = L"button2"; this->button2->Size = System::Drawing::Size(166, 39); this->button2->TabIndex = 6; this->button2->Text = L"测 试"; this->button2->UseVisualStyleBackColor = false; this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click); // // button3 // this->button3->BackColor = System::Drawing::Color::Cyan; this->button3->Font = (gcnew System::Drawing::Font(L"宋体", 14)); this->button3->Location = System::Drawing::Point(683, 117); this->button3->Name = L"button3"; this->button3->Size = System::Drawing::Size(166, 39); this->button3->TabIndex = 7; this->button3->Text = L"重 命 名"; this->button3->UseVisualStyleBackColor = false; this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click); // // label1 // this->label1->AutoSize = true; this->label1->Font = (gcnew System::Drawing::Font(L"宋体", 10)); this->label1->Location = System::Drawing::Point(23, 8); this->label1->Name = L"label1"; this->label1->Size = System::Drawing::Size(63, 14); this->label1->TabIndex = 8; this->label1->Text = L"文件路径"; // // label2 // this->label2->AutoSize = true; this->label2->Font = (gcnew System::Drawing::Font(L"宋体", 10)); this->label2->Location = System::Drawing::Point(23, 62); this->label2->Name = L"label2"; this->label2->Size = System::Drawing::Size(91, 14); this->label2->TabIndex = 9; this->label2->Text = L"文件名通配符"; // // label3 // this->label3->AutoSize = true; this->label3->Font = (gcnew System::Drawing::Font(L"宋体", 10)); this->label3->Location = System::Drawing::Point(299, 62); this->label3->Name = L"label3"; this->label3->Size = System::Drawing::Size(119, 14); this->label3->TabIndex = 10; this->label3->Text = L"正则截取 表达式"; // // label4 // this->label4->AutoSize = true; this->label4->Font = (gcnew System::Drawing::Font(L"宋体", 10)); this->label4->Location = System::Drawing::Point(704, 62); this->label4->Name = L"label4"; this->label4->Size = System::Drawing::Size(91, 14); this->label4->TabIndex = 11; this->label4->Text = L"新文件名样式"; // // Form1 // this->AutoScaleDimensions = System::Drawing::SizeF(6, 12); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(192)), static_cast<System::Int32>(static_cast<System::Byte>(255)), static_cast<System::Int32>(static_cast<System::Byte>(255))); this->ClientSize = System::Drawing::Size(951, 539); this->Controls->Add(this->label4); this->Controls->Add(this->label3); this->Controls->Add(this->label2); this->Controls->Add(this->label1); this->Controls->Add(this->button3); this->Controls->Add(this->button2); this->Controls->Add(this->button1); this->Controls->Add(this->textBox5); this->Controls->Add(this->textBox4); this->Controls->Add(this->textBox3); this->Controls->Add(this->textBox2); this->Controls->Add(this->textBox1); this->MaximumSize = System::Drawing::Size(967, 578); this->MinimumSize = System::Drawing::Size(967, 578); this->Name = L"Form1"; this->Text = L"文件重命名"; this->ResumeLayout(false); this->PerformLayout(); } #pragma endregion private: array <String^>^ fileEntries; private: Regex^ FileName; private: void ScanMyDir(String^ _path, String^ wildcard) { FileName = gcnew Regex(textBox3->Text); //Regex^ FileName = gcnew Regex(textBox3->Text); fileEntries = Directory::GetFiles(_path, wildcard); for each (auto i in fileEntries) { textBox5->Text += i->Substring(i->LastIndexOf("\\") + 1) + " -> " + FileName->Replace(i->Substring(i->LastIndexOf("\\") + 1), textBox4->Text) + "\r\n"; } } private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) { textBox5->Clear(); ScanMyDir(textBox1->Text, textBox2->Text); } private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { if (folderBrowserDialog1->ShowDialog() == Windows::Forms::DialogResult::OK) { textBox1->Text = folderBrowserDialog1->SelectedPath; } } private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) { textBox5->Clear(); for each (auto i in fileEntries) { String^ newname = FileName->Replace(i->Substring(i->LastIndexOf("\\") + 1), textBox4->Text); textBox5->Text += newname + "\r\n"; FileSystem::Rename(i, textBox1->Text + "\\" + newname); } } }; }
CLI_RENAME.rar
(33.61 KB)
[此贴子已经被作者于2023-2-24 13:03编辑过]