批量增加脚本命名空间
来源:互联网 发布:阿里云病毒入侵 编辑:程序博客网 时间:2024/05/19 23:02
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;
public class Test : MonoBehaviour
{
public static string startStr = "\nnamespace Test{";
public static string endStr = "\n}";
public static string checkStr = "public class";
public static string spaceName = "namespace Test";
public static string rootName = "/Scripts";
[MenuItem("Assets/AutoAddNameSpace")]
public static void AutoAddNameSpace()
{
CheckFileName(Application.dataPath + rootName);
}
public static void CheckFileName(string dataPath)
{
foreach (var item in Directory.GetFiles(dataPath))
{
if (item.Contains(".cs") && !item.Contains(".meta"))
{
if (!ContainNameSpace(item))
{
AddNameSpace(item);
}
}
}
foreach (var item in Directory.GetDirectories(dataPath))
{
CheckFileName(item);
}
}
public static bool ContainNameSpace(string path)
{
StreamReader sr_check = new StreamReader(path);
string all = sr_check.ReadToEnd();
sr_check.Close();
return all.Contains(spaceName);
}
public static void AddNameSpace(string path)
{
StreamReader sr = new StreamReader(path);
StringBuilder sb = new StringBuilder();
string line;
int lineIndex = 0;
while ((line = sr.ReadLine()) != null)
{
if (line.Contains(checkStr))
{
sb.Append(startStr);
}
if (lineIndex == 0)
sb.Append(line);
else
sb.Append("\n" + line);
lineIndex++;
}
sb.Append(endStr);
sr.Close();
StreamWriter sw = new StreamWriter(path);
sw.WriteLine(sb.ToString());
sw.Close();
}
using System.Text;
using UnityEditor;
using UnityEngine;
public class Test : MonoBehaviour
{
public static string startStr = "\nnamespace Test{";
public static string endStr = "\n}";
public static string checkStr = "public class";
public static string spaceName = "namespace Test";
public static string rootName = "/Scripts";
[MenuItem("Assets/AutoAddNameSpace")]
public static void AutoAddNameSpace()
{
CheckFileName(Application.dataPath + rootName);
}
public static void CheckFileName(string dataPath)
{
foreach (var item in Directory.GetFiles(dataPath))
{
if (item.Contains(".cs") && !item.Contains(".meta"))
{
if (!ContainNameSpace(item))
{
AddNameSpace(item);
}
}
}
foreach (var item in Directory.GetDirectories(dataPath))
{
CheckFileName(item);
}
}
public static bool ContainNameSpace(string path)
{
StreamReader sr_check = new StreamReader(path);
string all = sr_check.ReadToEnd();
sr_check.Close();
return all.Contains(spaceName);
}
public static void AddNameSpace(string path)
{
StreamReader sr = new StreamReader(path);
StringBuilder sb = new StringBuilder();
string line;
int lineIndex = 0;
while ((line = sr.ReadLine()) != null)
{
if (line.Contains(checkStr))
{
sb.Append(startStr);
}
if (lineIndex == 0)
sb.Append(line);
else
sb.Append("\n" + line);
lineIndex++;
}
sb.Append(endStr);
sr.Close();
StreamWriter sw = new StreamWriter(path);
sw.WriteLine(sb.ToString());
sw.Close();
}
}
使用完流之后 需要将流关闭,否则会出现无法访问文件的情况。因为此时文件正在被流占用。
阅读全文
0 0
- 批量增加脚本命名空间
- VS2005 增加命名空间
- 批量修改命名空间方法
- shell脚本批量增加用户
- php5增加的命名空间以及异常
- memcache批量删除之命名空间法
- 一键增加swap空间脚本
- 快速的批量修改重命名.net程序的命名空间
- 快速的批量修改重命名.net程序的命名空间
- 快速的批量修改重命名.net程序的命名空间
- 快速的批量修改重命名.net程序的命名空间
- 快速的批量修改重命名.net程序的命名空间
- 快速的批量修改重命名.net程序的命名空间
- Linux里重新命名批量文件的shell脚本
- 在运行时脚本中使用UnityEditor命名空间
- Oracle监视表空间,并自动增加数据文件脚本
- 批量处理时临时增加回滚表空间临时表空间文件
- 命名空间
- 线性回归----最小二乘法
- ROS引用其他package中库文件
- 拉格朗日对偶
- react-native-quan代码分析笔记
- 华展云-让展览更高效 2017第6届中国国防信息化装备与技术博览会会刊(参展商名录)
- 批量增加脚本命名空间
- 用SPK技术分析,泰坦尼克号沉船之后哪些人活下来了
- mysql和oracle的分页
- dubbo_zookeeper简单实例
- C++ set集合
- 快速排序算法
- 概要设计与详细设计的区别(转)
- JFreeChart中的时序图
- Dubbo基本原理机制