private void OnSaveClick(object sender, RoutedEventArgs e) { string location = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); string filename = Path.GetFileNameWithoutExtension(openFile); string savepath = Path.Combine(location, "output"); string fullpath = Path.Combine(savepath, filename + ".srt"); if (Directory.Exists(savepath) == false) { Directory.CreateDirectory(savepath); } using (FileStream stream = new FileStream(fullpath, FileMode.Create, FileAccess.Write, FileShare.Read)) { using (StreamWriter writer = new StreamWriter(stream, Encoding.Unicode)) { for (int i = 0; i < lvSubtitle.Items.Count; i++) { SubtitleObject subtitleObject = lvSubtitle.Items[i] as SubtitleObject; writer.WriteLine(subtitleObject.Index.ToString()); writer.WriteLine(String.Format("{0} --> {1}", subtitleObject.Start.ToString(@"hh\:mm\:ss\,fff"), subtitleObject.Stop.ToString(@"hh\:mm\:ss\,fff"))); writer.WriteLine(subtitleObject.Text); writer.WriteLine(); } } } }
private void FillToListView(Stream stream) { SubtitleObject subtitleObject = new SubtitleObject(); List <List <String> > sections = new List <List <String> >(); int number = 1; string strNumber = number.ToString(); stream.Seek(0, SeekOrigin.Begin); using (StreamReader reader = new StreamReader(stream, Encoding.Unicode)) { List <String> section = null; while (reader.EndOfStream == false) { string line = reader.ReadLine(); if (strNumber == line) { section = new List <String>(); sections.Add(section); number++; strNumber = number.ToString(); } section.Add(line); } } lvSubtitle.Items.Clear(); foreach (var section in sections) { if (section[section.Count - 1] == "") { section.RemoveAt(section.Count - 1); } Section currentSection = Section.INDEX; StringBuilder text = new StringBuilder(); foreach (string line in section) { if (currentSection == Section.INDEX) { subtitleObject.Index = Int32.Parse(line); currentSection = Section.TIME; } else if (currentSection == Section.TIME) { string[] fields = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); subtitleObject.Start = TimeSpan.Parse(fields[0].Replace(',', '.')); subtitleObject.Stop = TimeSpan.Parse(fields[2].Replace(',', '.')); currentSection = Section.TEXT; } else if (currentSection == Section.TEXT) { if (text.Length != 0) { text.AppendLine(); } text.Append(line); } } subtitleObject.Text = text.ToString(); lvSubtitle.Items.Add(subtitleObject); subtitleObject = new SubtitleObject(); } }