源代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;
using System.Security.AccessControl;
using System.Security.Principal;
namespace packagehandler
{
class Program
{
static void Main(string[] args)
{
//foreach (string arg in args){
// Console.WriteLine(arg);
//}
Dictionary cmdsKey = new Dictionary();
Dictionary cmds = new Dictionary();
cmdsKey["-i"] = true;
cmdsKey["-o"] = true;
cmds["-i"] = @"c:\11000";
for (int i = 0; i < args.Length; i++)
{
string arg = args[i];
if ((args.Length - i) < 2) continue;
if (cmdsKey[arg.ToLower()]) cmds[arg.ToLower()] = args[(++i)];
}
if (!cmds.ContainsKey("-i"))
{
Console.WriteLine("-i cmd no found~");
Console.WriteLine("ef:");
Console.WriteLine("package.exe -i inputPath [-o outputPath]");
Console.WriteLine("press any key to exit");
Console.Read();
return;
}
string inputPath = cmds["-i"];
string outputPath;
if (!cmds.ContainsKey("-o"))
{
outputPath = inputPath + "_o";
cmds["-o"] = outputPath;
}
else
{
outputPath = cmds["-o"];
}
foreach (KeyValuePair var in cmds)
{
Console.WriteLine(var.Key + ":" + cmds[var.Key]);
}
Dictionary nameMap = new Dictionary();
nameMap["attack1"] = "1";
nameMap["attack2"] = "2";
nameMap["attacked"] = "3";
nameMap["defend"] = "4";
nameMap["ready"] = "5";
nameMap["run"] = "6";
ReplaceDirsNameHandler replaceDirsNameCallback = delegate(string path,ReplaceDirsNameHandler loop)
{
string[] dirStrArr = Directory.GetDirectories(path);
foreach (string dir in dirStrArr)
{
if (Directory.Exists(dir))
{
DirectoryInfo di = new DirectoryInfo(dir);
//loop
loop(di.FullName, loop);
//Console.WriteLine(di.Name);
//不存在名称map里
if (!nameMap.ContainsKey(di.Name)) continue;
string newpath = outputPath + "\\" + di.Parent.Name + "\\";
string newName = newpath + nameMap[di.Name];
//if (!Directory.Exists(newpath)) Directory.CreateDirectory(newpath);
//重命名
di.MoveTo(newName);
}
}
};
Directory.Delete(outputPath, true);
CopyDir(inputPath, outputPath);
replaceDirsNameCallback(outputPath, replaceDirsNameCallback);
Console.WriteLine("dirs name replace complete!");
Console.Read();
}
static bool CopyDir(string srcPath, string aimPath)
{
try
{
// 检查目标目录是否以目录分割字符结束如果不是则添加之
if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)
aimPath += Path.DirectorySeparatorChar;
// 判断目标目录是否存在如果不存在则新建之
if (!Directory.Exists(aimPath))
Directory.CreateDirectory(aimPath);
// 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
// 如果你指向copy目标文件下面的文件而不包含目录请使用下面的方法
string[] fileList = Directory.GetFileSystemEntries(srcPath);
// 遍历所有的文件和目录
foreach (string file in fileList)
{
// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
if (Directory.Exists(file))
{
CopyDir(file, aimPath + Path.GetFileName(file));
}
// 否则直接Copy文件
else
{
File.Copy(file, aimPath + Path.GetFileName(file), true);
}
}
return true;
}
catch
{
return false;
}
}
internal delegate void ReplaceDirsNameHandler(string path,ReplaceDirsNameHandler loop);
}
}