using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Text; using System.Windows.Forms; namespace MOTINOVA_Motor_Factory_Set { public partial class ManageForm : Form { public string FilePath = "";//文件路径 public ManageForm() { InitializeComponent(); } private void ManageForm_Load(object sender, EventArgs e) { groupBox_File.Visible = true; button_Delete.Visible = true; button_OpenFile.Visible = true; richTextBox1.Visible = false; if (System.IO.Directory.Exists(FilePath) == false) System.IO.Directory.CreateDirectory(FilePath); ListDate(FilePath); } private void ListDate(string path) { if (System.IO.Directory.Exists(path) == false) { System.IO.Directory.CreateDirectory(path); } //装载配置文件日期列表 string[] DateList = { "" }; DateList = System.IO.Directory.GetDirectories(path); comboBox_Date.Items.Clear(); foreach (var file in DateList) { comboBox_Date.Items.Add(file.Substring(file.LastIndexOf("\\") + 1)); } } private void ListFileRefresh(string path) { var files = Directory.GetFiles(path, "*.ttcfg"); listBox_File.Items.Clear(); foreach (var file in files) { listBox_File.Items.Add(Path.GetFileName(file)); } if (listBox_File.Items.Count != 0) listBox_File.SelectedIndex = 0; } private void button_Delete_Click(object sender, EventArgs e) { try { string SelectFile; SelectFile = listBox_File.SelectedItem.ToString(); //删除选中文件 if (MessageBox.Show("确认删除?", "确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) { File.Delete(FilePath + "\\" + comboBox_Date.Text + "\\" + SelectFile); //删除其它相关文件 string extFileName = ""; extFileName = SelectFile.Split('_')[0] + "_" + SelectFile.Split('_')[1] + "_" + SelectFile.Split('_')[6] + "_" + (SelectFile.Split('_')[7].Substring(0, SelectFile.Split('_')[7].LastIndexOf(".")) + ".pdf"); File.Delete(FilePath + "\\" + comboBox_Date.Text + "\\" + extFileName); extFileName = SelectFile.Split('_')[0] + "_" + SelectFile.Split('_')[1] + "_" + SelectFile.Split('_')[6] + "_" + (SelectFile.Split('_')[7].Substring(0, SelectFile.Split('_')[7].LastIndexOf(".")) + ".xls"); File.Delete(FilePath + "\\" + comboBox_Date.Text + "\\" + extFileName); if (System.IO.Directory.Exists(FilePath) == false) System.IO.Directory.CreateDirectory(FilePath); ListFileRefresh(FilePath + "\\" + comboBox_Date.Text); } } catch { MessageBox.Show("请选择文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } finally { } } private void button_OpenFile_Click(object sender, EventArgs e) { if (button_OpenFile.Text == "查看") { button_OpenFile.Text = "隐藏"; button_Delete.Visible = false; richTextBox1.Visible = true; richTextBox1.Text = ""; //读取选择文件 try { string SelectFile; if (listBox_File.Items.Count == 0) { MessageBox.Show("无可用文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); button_OpenFile.Text = "查看"; button_Delete.Visible = true; richTextBox1.Visible = false; richTextBox1.Text = ""; return; } if (listBox_File.SelectedItems.Count == 0) { MessageBox.Show("请选择文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); button_OpenFile.Text = "查看"; button_Delete.Visible = true; richTextBox1.Visible = false; richTextBox1.Text = ""; return; } SelectFile = listBox_File.SelectedItem.ToString(); richTextBox1.Text += "文件名称:" + SelectFile + "\r\n"; //打开选中文件 StreamReader objReader = new StreamReader(FilePath + "\\" + comboBox_Date.Text + "\\" + SelectFile); string sLine = ""; ArrayList arrText = new ArrayList();//创建一个动态数组 while (sLine != null) { sLine = objReader.ReadLine(); arrText.Add(sLine); } objReader.Close(); richTextBox1.Text = ""; foreach (string sOutput in arrText) { richTextBox1.Text += sOutput + "\r\n"; } } catch { MessageBox.Show("请选择文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); button_OpenFile.Text = "查看"; button_Delete.Visible = true; richTextBox1.Visible = false; richTextBox1.Text = ""; } finally { } } else if (button_OpenFile.Text == "隐藏") { button_OpenFile.Text = "查看"; button_Delete.Visible = true; richTextBox1.Visible = false; richTextBox1.Text = ""; } } private void comboBox_Date_SelectionChangeCommitted(object sender, EventArgs e) { ListFileRefresh(FilePath + "\\" + comboBox_Date.SelectedItem.ToString()); } private void button_SetPath_Click(object sender, EventArgs e) { string path = textBox_LocalPath.Text; string cfg_file = System.IO.Directory.GetCurrentDirectory() + "\\UserPath"; StreamReader objReader = new StreamReader(cfg_file); ArrayList arrText = new ArrayList();//创建一个动态数组 objReader.Close(); arrText.Add("logPath =" + path); using (StreamWriter file = new StreamWriter(cfg_file)) { foreach (var entry in arrText) { file.WriteLine(entry); } } MessageBox.Show("路径已设置,软件重启后生效!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } private void textBox_LocalPath_Click(object sender, EventArgs e) { textBox_LocalPath.SelectAll(); } } }