MSBuild Concat任务仅处理ASCII文件

时间:2010-08-08 03:10:11

标签: msbuild

我正在使用MSBuild副本,然后使用Concat连接多个SQL文件。一些SQL文件采用其他格式(在记事本中打开),但在Concat访问文件后,生成的文件中都会出现奇怪的字符。

任何人都可以帮助我,有没有人经历过这个?如何将所有SQL文件转换为ascii,或使Concat任务正常工作?

1 个答案:

答案 0 :(得分:1)

我通过编写实用程序C#类来解决它。它不是为处理异常而设计的,它们由调用类处理或在控制台中未处理。

它将文件转换为ASCII,删除所有无效字符并重写文件。

我花了一段时间想知道为什么我会收到拒绝访问权限的问题,请确保在转换之前检出所有要转换的文件!

如果您有兴趣,请点击这里:

using System;
using System.IO;
using System.Text;

/// <summary>
/// Manages encoding of text files.
/// </summary>
public class TextFileEncoding
{
    /// <summary>
    /// Main command-line entry.
    /// </summary>
    /// <param name="args"></param>
    public static void Main(string[] args)
    {
        if (args.Length > 0)
        {
            string argParam = args[0].ToString().Trim().ToLower();
            switch (argParam)
            {
                // Return encoding for a file.
                case "get":
                    Encoding enc = GetEncoding(args[1].ToString());
                    Console.WriteLine(enc.EncodingName);
                    break;

                // convert encoding for a path with a given pattern to ASCII.
                case "toascii":
                    int count = ConvertToAscii(args[1].ToString(), args.Length == 3 ? args[2] : "*.*");
                    Console.WriteLine("Successfully converted " + count + " files.");
                    break;
                default:
                    Console.WriteLine("Invalid parameter. Expected get or toascii");
                    break;
            }
        }
        else
        {
            Console.WriteLine("Missing filename parameter.\nFormat: TextFileEncoding.exe [get|toascii] <TextFile> <Path> <Pattern>");
        }
    }

    /// <summary>
    /// Returns the encoding of the filename at the specified path.
    /// </summary>
    /// <param name="filename"></param>
    /// <returns></returns>
    public static Encoding GetEncoding(string filename)
    {
        StreamReader fileStream = new StreamReader(filename, true);
        return GetEncoding(fileStream);
    }

    /// <summary>
    /// Returns the encoding of the file stream.
    /// </summary>
    /// <param name="fileStream"></param>
    /// <returns></returns>
    public static Encoding GetEncoding(StreamReader fileStream)
    {
        return fileStream.CurrentEncoding;
    }

    /// <summary>
    /// Converts all files to ascii encoding.
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public static int ConvertToAscii(string path, string pattern)
    {
        DirectoryInfo pathInfo = new DirectoryInfo(path);
        int count = 0;

        // Get files in directory, recursively.
        FileInfo[] files = pathInfo.GetFiles(pattern, SearchOption.AllDirectories);

        foreach (FileInfo file in files)
        {
            // Encode to ASCII.
            if (EncodeAscii(file))
            {
                count++;
            }
        }

        return count;
    }

    /// <summary>
    /// Converts file to ascii.
    /// </summary>
    /// <param name="file"></param>
    /// <returns></returns>
    private static bool EncodeAscii(FileInfo file)
    {
        ASCIIEncoding encoder = new ASCIIEncoding();

        // Get content of file as string.
        string content = File.ReadAllText(file.FullName);

        // Convert to an ASCII byte array.
        byte[] asciiString = encoder.GetBytes(content);

        // Convert to string, so all unknown characters get removed.
        string cleanString = encoder.GetString(asciiString);

        // Convert back to byte array for writing to file.
        byte[] cleanBytes = encoder.GetBytes(cleanString);

        // Delete and rewrite file as ASCII.
        file.Delete();
        File.WriteAllBytes(file.FullName, cleanBytes);

        return true;
    }
}