/// <summary> /// 实例化文件保存对话框保存文本 /// </summary> /// <param name="t">要保存内容的文本框</param> /// <returns></returns> public static object saveFileMethod(TextBox t) { SaveFileDialog newSaveFile = new SaveFileDialog(); Dictionary <string, object> textTag = TextBoxUtils.GetTextTagToMap(t); newSaveFile.RestoreDirectory = false; newSaveFile.ValidateNames = true; newSaveFile.DefaultExt = "txt"; if (t != null && t.Parent != null) { newSaveFile.FileName = t.Parent.Text; } newSaveFile.Filter = "文本文档(*.txt)|*.txt|所有文件(*.*)|*.*"; // 获取文本框保存的Ecoding Encoding encoding = Encoding.Default; if (textTag.ContainsKey(TextBoxTagKey.TEXTBOX_TAG_KEY_ECODING) && textTag[TextBoxTagKey.TEXTBOX_TAG_KEY_ECODING] is Encoding) { encoding = (Encoding)textTag[TextBoxTagKey.TEXTBOX_TAG_KEY_ECODING]; } //判断是否点击确定 if (newSaveFile.ShowDialog() == DialogResult.OK) { string path = newSaveFile.FileName; // 调用方法写入文件内容 FileUtils.FileWrite.WriteFile(path, t.Text, encoding); // 将保存路径加入到文本框的Tag属性 TextBoxUtils.TextBoxAddTag(t, TextBoxTagKey.SAVE_FILE_PATH, newSaveFile.FileName); // 监听文件变化并弹窗提醒 传入的文本框为null则开启一个新标签 TextBox tempTextB = t != null? t : MainTabControlUtils.GetNewPageTextBox(); FileUtils.SetTextBoxValByPath(tempTextB, path, encoding); } return(newSaveFile); }
// 窗口加载时判断是否有拖动到上面的文件 private void loadOpenFile() { if (loadPath != null && loadPath.Length > 0) { foreach (string p in loadPath)// 遍历路径 { FileUtils.SetTextBoxValByPath(MainTabControlUtils.GetNewPageTextBox(), p, Encoding.UTF8); } } }
/// <summary> /// 实例化文本选择对话框 /// </summary> /// <param name="t">为null则打开一个新标签</param> /// <returns>该对话框</returns> public static object openFileMethod(TextBox t) { OpenFileDialog openFile = new OpenFileDialog(); openFile.CheckFileExists = true; openFile.DefaultExt = "txt"; openFile.Filter = "文本文档(*.txt)|*.txt|所有文件(*.*)|*.*"; openFile.DereferenceLinks = true; openFile.Multiselect = true; openFile.RestoreDirectory = false; // 判断是否点击确定 if (openFile.ShowDialog() == DialogResult.OK) { string path = openFile.FileName; // 判断编码 Encoding encoding = FileUtils.GetType(path); TextBox tempTextB = t != null? t : MainTabControlUtils.GetNewPageTextBox(); FileUtils.SetTextBoxValByPath(tempTextB, path, encoding); } return(openFile); }