/// <summary> /// 通过路径获取工作空间 /// </summary> /// <param name="eType">要打开的工作空间类别</param> /// <param name="path">文件或文件夹路径</param> /// <returns></returns> public static IWorkspace GetWorkspaceFromFile(string path, EWorkspaceType eType) { try { IWorkspaceName workspaceName = new WorkspaceNameClass(); workspaceName.WorkspaceFactoryProgID = eType.GetDescription(1); workspaceName.PathName = path; IName iName = (IName)workspaceName; IWorkspace workspace = iName.Open() as IWorkspace; Marshal.ReleaseComObject(iName); Marshal.ReleaseComObject(workspaceName); return(workspace); } catch (Exception ex) { var msg = ex.Message; //判断路径长度是否符合要求(Windows默认的字符的长度限制为260,一个中文字符长度为2)(路径超长不一定出错,mdb数据会出错,shp数据不一定出错) if (!FileOpt.PathLengthValidate(path, out var length)) { msg += $"\r\n 可能原因为:路径长度超出限制,无法识别“{path}”的数据,请修改数据存放路径\r\n(允许路径最大长度为260,该路径长度为{length})\r\n"; } throw new Exception($"打开{eType.GetDescription(2)}工作空间“{path}”出错:{msg}"); } }
/// <summary> /// 根据PropertiesList文件,获取配置文件名的List /// </summary> /// <param name="filePath"></param> /// <param name="fileType"></param> /// <returns></returns> private string[] GetPropertiesList(string filePath, string fileType) { string text = FileOpt.ReadFile(filePath); text = text.Replace("\r", ""); string[] lists = text.Split('\n'); int index = 0; while (!lists[index].Contains(fileType)) { index++; } List <string> result = new List <string>(); for (int i = index + 1; i < lists.Length; i++) { if (String.IsNullOrEmpty(lists[i])) { continue; } if (lists[i].Contains("[")) { break; } result.Add(lists[i]); } return(result.ToArray()); }
/// <summary> /// 通过反射创建插件中的命令(ICommand),设置命令的传入参数 /// </summary> /// <typeparam name="TCmdData"></typeparam> /// <param name="plugin"></param> /// <param name="inputData"></param> /// <returns></returns> public static ICommand CreateCommand<TCmdData>(this IPluginItem plugin, TCmdData inputData) { var assemblyPath = FileOpt.GetRootPath(plugin.AssemblyPath); var objCmd = Assembly.LoadFile(assemblyPath).CreateInstance(plugin.TypeName); var cmd = objCmd as ICommand<TCmdData>; cmd.InputData = inputData; return plugin.Command = cmd; }
public void SimplifyFile(string filePath) { string xmlName = Path.GetFileName(filePath); Console.WriteLine(xmlName); string xmlText = FileOpt.ReadText(filePath); string content = SimplifyXmlText(xmlText); content = AddHeader(content); FileOpt.WriteText(outDir + xmlName, content); }
public void Save(string path) { string content = ""; foreach (string md5 in md5s) { content += md5 + "\n"; } content.Trim('\n'); FileOpt.WriteText(content, path); }
public static MD5Check GenFromFile(string path) { MD5Check check = new MD5Check(); string content = FileOpt.ReadTextFromFile(path); if (!string.IsNullOrEmpty(content)) { string[] items = content.Split('\n'); check.md5s.AddRange(items); } return(check); }
public void LoadByLocal() { if (!File.Exists(ConstData.IDLstPath) || !File.Exists(ConstData.IDLstPath2) || !File.Exists(ConstData.IDLstPath3) || !File.Exists(ConstData.IDLstPath4)) { LoadByNet(); } else { lstCode.Clear(); lstCode2.Clear(); lstCode3.Clear(); lstCode4.Clear(); string contents = FileOpt.ReadFileTxt(ConstData.IDLstPath); string[] array = contents.Split('\t'); for (int i = 0; i < array.Length; ++i) { lstCode.Add(array[i]); } contents = FileOpt.ReadFileTxt(ConstData.IDLstPath2); array = contents.Split('\t'); for (int i = 0; i < array.Length; ++i) { lstCode2.Add(array[i]); } contents = FileOpt.ReadFileTxt(ConstData.IDLstPath3); array = contents.Split('\t'); for (int i = 0; i < array.Length; ++i) { lstCode3.Add(array[i]); } contents = FileOpt.ReadFileTxt(ConstData.IDLstPath4); array = contents.Split('\t'); for (int i = 0; i < array.Length; ++i) { lstCode4.Add(array[i]); } } }
public static void AddItemToXml <T>(T[] infos) { Type t = typeof(T); FieldInfo[] fields = t.GetFields(); string insertTag = "</Table>"; string filePath = "Resources/xml/" + t.Name + ".xml"; string content = FileOpt.ReadFile(filePath); int insertIndex = content.IndexOf(insertTag); string s_pre = content.Substring(0, insertIndex); string s_tail = content.Substring(insertIndex, content.Length - insertIndex); //<Row ss:AutoFitHeight="0"> // <Cell><Data ss:Type="String">int</Data></Cell> // <Cell><Data ss:Type="String">Vector3</Data></Cell> // <Cell><Data ss:Type="String">string</Data></Cell> //</Row> string insertStr = ""; for (int k = 0; k < infos.Length; k++) { T info = infos[k]; insertStr += " <Row>\n"; for (int i = 0; i < fields.Length; i++) { FieldInfo fi = fields[i]; insertStr += " <Cell><Data ss:Type=\"String\">" + fi.GetValue(info) + "</Data></Cell>\n"; } insertStr += " </Row>\n"; } content = s_pre + insertStr + s_tail; int fixIndex = content.IndexOf("ss:ExpandedRowCount"); if (fixIndex >= 0) { int endFixInde = content.IndexOf(" ", fixIndex); content = content.Substring(0, fixIndex) + content.Substring(endFixInde, content.Length - endFixInde); } FileOpt.WriteFile(filePath, content); }
/// <summary> /// 将命令转为插件 /// </summary> /// <param name="cmd">通过反射获取的功能命令</param> /// <param name="appPath">插件所属应用软件的位置,插件的命令位置将设置为对于应用程序的相对路径</param> /// <returns></returns> public static PluginItem FromCommand(ICommand cmd, string appPath) { var type = cmd.GetType(); var assemblyPath = FileOpt.GetRelativePath(type.Assembly.Location, appPath); return(new PluginItem { Id = Guid.NewGuid().ToString(), Name = cmd.Name, Text = cmd.Text, AssemblyPath = assemblyPath, TypeName = type.FullName, Tips = cmd.Description, Visible = true, InvokType = EPluginInvokeType.OnClick, Command = cmd, }); }
private void SimplifyOnlyLangs(string filePath) { FileOpt.ClearFolder(outDir, "languages.xml"); string xmlName = Path.GetFileName(filePath); Console.WriteLine(xmlName); string xmlText = FileOpt.ReadText(filePath); string content = SimplifyXmlText(xmlText); content = AddHeader(content); Dictionary <string, string> spContent = SplitLang(content); foreach (var data in spContent) { string fPath = outDir + FormatXmlName(data.Key); FileOpt.WriteText(fPath, data.Value); } }
static List <string> GetImportProtoNames(string dir, string protoName) { List <string> imports = new List <string>(); string text = FileOpt.ReadTextFromFile(Path.Combine(dir, protoName)); string[] lines = text.Split('\n'); foreach (string line in lines) { string str = line.Trim(); if (str.StartsWith("import")) { int st = str.IndexOf("\"") + 1; int ed = str.IndexOf("\"", st); string importName = str.Substring(st, ed - st); imports.Add(importName); } } return(imports); }
public static void ClearXmlData <T>() { Type t = typeof(T); string filePath = "Resources/Properties/" + t.Name + ".xml"; string content = FileOpt.ReadFile(filePath); int index = 0; for (int i = 0; i < 3; i++) { if (index >= 0) { index = content.IndexOf("</Row>", index + 6); } } if (index >= 0) { index += 7; int lastIndex = content.IndexOf("</Table>", index); content = content.Substring(0, index) + content.Substring(lastIndex, content.Length - lastIndex); } FileOpt.WriteFile(filePath, content); }
private void TreeViewMenus_DragDrop(object sender, DragEventArgs e)//在完成拖放操作时 { var targeTreeView = (TreeView)sender; var sourceNode = (TreeNode)e.Data.GetData(typeof(TreeNode));//获得拖动过来的节点 if (sourceNode == null) { return; } var point = targeTreeView.PointToClient(new Point { X = e.X, Y = e.Y }); var targetNode = targeTreeView.GetNodeAt(point);//根据鼠标坐标获得目标节点 if (targetNode == null) { return; } var tag = sourceNode.Tag; var pluginItem = tag is ICommand cmd?PluginItem.FromCommand(cmd, PluginDir) : tag as PluginItem; //特殊处理:由于Assembly.LoadFrom方法反射程序集A会被缓存, //随后再反射同名但路径不同的程序集B,因缓存原因实际反射的仍是程序集A的奇葩机制,目前未找到解决方案,此处只能强行替代程序集的路径 pluginItem.AssemblyPath = Path.Combine(PluginDir, Path.GetFileName(pluginItem.AssemblyPath)); pluginItem.AssemblyPath = FileOpt.GetRelativePath(pluginItem.AssemblyPath, PluginDir); var insertNode = pluginItem.CreateNode(4); AppendToMenu(targetNode, insertNode); if (sourceNode.TreeView == targeTreeView) { sourceNode.Remove(); } targeTreeView.SelectedNode = insertNode; }
/// <summary> /// 检查路径文件是否被占用 /// </summary> /// <param name="path"></param> /// <returns>路径文件存在且被占用则返回提示信息,否则返回null</returns> public static string CheckPathLock(string path) { return(File.Exists(path) && FileOpt.FileIsUsed(path) ? "文件被占用,请关闭占用该文件的相关软件,例如ArcMap、ArcCatlog、其他使用数据的程序" : null); }
public static string FileMD5(string filePath) { return(MD5Encrypt(FileOpt.ReadTextFromFile(filePath))); }
public void LoadByNet() { lstCode.Clear(); lstCode2.Clear(); lstCode3.Clear(); lstCode4.Clear(); // 1 string content = ""; for (int i = 600000; i < 606999; ++i) { string code = string.Format("{0:000000}", i); string result = GetHttpData.GetUrltoHtml(ConstData.UrlBaseSina + ConstData.sh + code); if (result != null && result.Length > 100) { lstCode.Add(code); } } for (int i = 0; i < lstCode.Count; ++i) { content += lstCode[i] + "\t"; } FileOpt.WriteFile(ConstData.IDLstPath, UTF8Encoding.UTF8.GetBytes(content)); // 2 content = ""; for (int i = 000001; i < 001999; ++i) { string code = string.Format("{0:000000}", i); string result = GetHttpData.GetUrltoHtml(ConstData.UrlBaseSina + ConstData.sz + code); if (result != null && result.Length > 100) { lstCode2.Add(code); } } for (int i = 0; i < lstCode2.Count; ++i) { content += lstCode2[i] + "\t"; } FileOpt.WriteFile(ConstData.IDLstPath2, UTF8Encoding.UTF8.GetBytes(content)); // 3 content = ""; for (int i = 002000; i < 002999; ++i) { string code = string.Format("{0:000000}", i); string result = GetHttpData.GetUrltoHtml(ConstData.UrlBaseSina + ConstData.sz + code); if (result != null && result.Length > 100) { lstCode3.Add(code); } } for (int i = 0; i < lstCode3.Count; ++i) { content += lstCode3[i] + "\t"; } FileOpt.WriteFile(ConstData.IDLstPath3, UTF8Encoding.UTF8.GetBytes(content)); // 4 content = ""; for (int i = 300001; i < 301999; ++i) { string code = string.Format("{0:000000}", i); string result = GetHttpData.GetUrltoHtml(ConstData.UrlBaseSina + ConstData.sz + code); if (result != null && result.Length > 100) { lstCode4.Add(code); } } for (int i = 0; i < lstCode4.Count; ++i) { content += lstCode4[i] + "\t"; } FileOpt.WriteFile(ConstData.IDLstPath4, UTF8Encoding.UTF8.GetBytes(content)); }
public static string Format(string path) { return(FormatContent(FileOpt.ReadTextFromFile(path))); }
/// <summary> /// 加载xml文本 /// </summary> /// <returns></returns> public string LoadXmlText(string path) { string text = FileOpt.ReadFile(path).Replace(":", replaceTag); return(text); }
public static void FormatAndSave(string srcPath, string savePath) { string content = Format(srcPath); FileOpt.WriteText(content, savePath); }
/// <summary> /// 检查文件名或目录是否合法,不能为空或空白字符,不能包含字符\/:*?"<>| /// </summary> /// <param name="path"></param> /// <returns>不合法则返回提示信息,否则返回null</returns> public static string CheckPathValidate(string path) { return(FileOpt.ValidFileName(path) && DirectoryOpt.ValidFolderName(path) ? null : "路径为空或包含特殊字符:\\/:*?\"&<>|"); }