Ass2Srt.AssWriter.WriteAssFile C# (CSharp) Method

WriteAssFile() public method

public WriteAssFile ( string strAssDescription, string strAssDialogues ) : bool
strAssDescription string
strAssDialogues string
return bool
        public bool WriteAssFile(string[] strAssDescription, string[] strAssDialogues)
        {
            CreateFolder();
            switch (type)
            {
                case 5:
                    strFileName += ".简体&英文";
                    break;
                case 6:
                    strFileName += ".繁体&英文";
                    break;
                case 7:
                    strFileName += ".简体";
                    break;
                case 8:
                    strFileName += ".繁体";
                    break;
                case 9:
                    strFileName += ".英文";
                    break;
            }

            strFileName += ".ass";

            try
            {
                fs = new FileStream(strNewFolderPath + "\\" + strFileName, FileMode.OpenOrCreate);
                //if (type == 0 || type == 2)
                //{
                //    sw = new StreamWriter(fs, Encoding.GetEncoding(936));
                //}
                //else if (type == 1 || type == 3)
                //{
                //    sw = new StreamWriter(fs, Encoding.GetEncoding(950));
                //}
                //else
                //{
                    //sw = new StreamWriter(fs, Encoding.Unicode);
                    //NOW USE UTF8 to make it work on UNIX-like systems.
                sw = new StreamWriter(fs, Encoding.UTF8);
                //}

                for(int i = 0; i < strAssDescription.Length - 1; i++)
                {
                    sw.WriteLine(strAssDescription[i]);
                    sw.Flush();
                }

                foreach(string s in strAssDialogues)
                {
                    sw.WriteLine(s);
                    sw.Flush();
                }

                fs.Close();
                return true;
            }

            catch(Exception)
            {
                return false;
            }
        }

Usage Example

Example #1
0
        private void bgwA2SOutput_DoWork(object sender, DoWorkEventArgs e)
        {
            A2SControlEnabled(false);

            string strDialogues;
            string strDescriptions;
            string strFolderPath;
            string strFileNameWithExtension;
            string strFileNameWithoutExtension;
            string strFilePath;
            string strDestDir = null;
            int fileNum = lvASSFileSelection.Items.Count;
            ssProgressBar.Maximum = fileNum;
            ssProgressBar.Minimum = 0;
            ssProgressBar.Step = 1;
            ssProgressBar.Value = 0;

            foreach (ListViewItem lviFile in lvASSFileSelection.Items)
            {
                strDestDir = tbSrtOutputFolder.Text;
                strFilePath = (string)lviFile.Tag;
                int lastSlash = strFilePath.LastIndexOf("\\");
                strFolderPath = strFilePath.Substring(0, lastSlash);
                strFileNameWithExtension = strFilePath.Substring(lastSlash + 1);

                int lastDot = strFileNameWithExtension.LastIndexOf(".");
                strFileNameWithoutExtension = strFileNameWithExtension.Substring(0, lastDot);

                AssReader ar = new AssReader(strFilePath);
                if (ar.IsValid())
                {
                    foreach (ListViewItem lviType in lvVersionSelection.CheckedItems)
                    {
                        if (lviType.Index <= 4)
                        {
                            strDialogues = ar.ReadDialogues();
                            AssAnalyzerForSrt aafs = new AssAnalyzerForSrt(strDialogues, lviType.Index, rbChinEng.Checked);
                            string[] strSrtLines = aafs.Analyze();
                            SrtWriter sw = new SrtWriter(strDestDir, strFileNameWithoutExtension, lviType.Index);
                            sw.WriteSrtFile(strSrtLines);
                        }

                        else
                        {
                            string[] strAssDialogues;
                            string[] strAssDescriptions;
                            strDialogues = ar.ReadDialogues();
                            strDescriptions = ar.ReadDescription();
                            AssAnalyzerForAss aafa = new AssAnalyzerForAss(strDialogues, strDescriptions, lviType.Index, rbChinEng.Checked);
                            aafa.Analyze(out strAssDescriptions, out strAssDialogues);
                            AssWriter aw = new AssWriter(strDestDir, strFileNameWithoutExtension, lviType.Index);
                            aw.WriteAssFile(strAssDescriptions, strAssDialogues);
                        }
                    }

                    ZipPatcher(strDestDir, strFileNameWithoutExtension);
                    ssProgressBar.PerformStep();
                }

                else
                {
                    MessageBox.Show("不是.ass文件!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            A2SControlEnabled(true);
        }