示例#1
0
        static void SetFileEncoding(string filePath, Encoding targetEncoding = null)
        {
            if (targetEncoding == null)
            {
                targetEncoding = Encoding.UTF8;
            }
            Encoding encoding = EncodingUtil.GetEncoding(filePath);
            //    LogCat.logError(file_path);
            //    LogCat.logError(target_encoding);
            var data = File.ReadAllBytes(filePath);

            data = targetEncoding.GetBytes(encoding.GetString(data));
            File.WriteAllBytes(filePath, data);
        }
示例#2
0
        public static ShellRequest ProcessCommand(string cmd, string workDirectory = StringConst.String_Empty,
                                                  List <string> environmentVars    = null)
        {
            ShellRequest shellRequest = new ShellRequest();

            ThreadPool.QueueUserWorkItem(delegate
            {
                Process process = null;
                try
                {
                    ProcessStartInfo processStartInfo = new ProcessStartInfo(shellApp);

#if UNITY_EDITOR_OSX
                    string splitChar = ":";
                    start.Arguments  = "-c";
#elif UNITY_EDITOR_WIN
                    string splitChar           = ";";
                    processStartInfo.Arguments = "/c";
#endif

                    if (environmentVars != null)
                    {
                        foreach (string var in environmentVars)
                        {
                            processStartInfo.EnvironmentVariables["PATH"] += (splitChar + var);
                        }
                    }

                    processStartInfo.Arguments       += (" \"" + cmd + " \"");
                    processStartInfo.CreateNoWindow   = true;
                    processStartInfo.ErrorDialog      = true;
                    processStartInfo.UseShellExecute  = false;
                    processStartInfo.WorkingDirectory = workDirectory;

                    if (processStartInfo.UseShellExecute)
                    {
                        processStartInfo.RedirectStandardOutput = false;
                        processStartInfo.RedirectStandardError  = false;
                        processStartInfo.RedirectStandardInput  = false;
                    }
                    else
                    {
                        processStartInfo.RedirectStandardOutput = true;
                        processStartInfo.RedirectStandardError  = true;
                        processStartInfo.RedirectStandardInput  = true;
                        processStartInfo.StandardOutputEncoding = Encoding.UTF8;
                        processStartInfo.StandardErrorEncoding  = Encoding.UTF8;
                    }

                    process = Process.Start(processStartInfo);
                    process.ErrorDataReceived += delegate(object sender, DataReceivedEventArgs e)
                    {
                        LogCat.LogError(EncodingUtil.GBK2UTF8(e.Data));
                    };
                    process.OutputDataReceived += delegate(object sender, DataReceivedEventArgs e)
                    {
                        LogCat.LogError(EncodingUtil.GBK2UTF8(e.Data));
                    };
                    process.Exited += delegate(object sender, EventArgs e) { LogCat.LogError(e.ToString()); };

                    bool is_has_error = false;
                    do
                    {
                        string line = process.StandardOutput.ReadLine();
                        if (line == null)
                        {
                            break;
                        }

                        line = line.Replace("\\", "/");
                        _queue.Add(delegate() { shellRequest.Log(0, line); });
                    } while (true);

                    while (true)
                    {
                        string error = process.StandardError.ReadLine();
                        if (string.IsNullOrEmpty(error))
                        {
                            break;
                        }

                        is_has_error = true;
                        _queue.Add(delegate() { shellRequest.Log(LogCatType.Error, error); });
                    }

                    process.Close();
                    if (is_has_error)
                    {
                        _queue.Add(delegate() { shellRequest.Error(); });
                    }
                    else
                    {
                        _queue.Add(delegate() { shellRequest.NotifyDone(); });
                    }
                }
                catch (Exception e)
                {
                    LogCat.LogError(e);
                    process?.Close();
                }
            });
            return(shellRequest);
        }
示例#3
0
文件: ByteUtil.cs 项目: uiopsczc/Test
        public static string ToString(byte[] data, int offset, int len, Encoding encoding = null)
        {
            var bb = data.Clone(offset, len);

            return(EncodingUtil.GetString(bb, offset, len, encoding));
        }