Ass2Srt.AssReader.IsValid C# (CSharp) Method

IsValid() public method

public IsValid ( ) : bool
return bool
        public bool IsValid()
        {
            try
            {
                fs = new FileStream(filePath, FileMode.Open);
                sr = new StreamReader(fs, true);

                if (fs.Length > 1024 * 1024 || fs.Length <= 0)    //文件不得大于1M
                {
                    return false;
                }

                else
                {
                    fileContent = sr.ReadToEnd();

                    if (fileContent.Contains("Dialogue:"))
                    {
                        return true;
                    }

                    else
                    {
                        return false;
                    }
                }
            }

            catch (Exception)
            {
                return false;
            }

            finally
            {
                fs.Close();
            }
        }

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);
        }