BF2Statistics.MapList.SaveToFile C# (CSharp) Method

SaveToFile() public method

Saves the current maplist to a Con file
public SaveToFile ( string FilePath ) : void
FilePath string The full path to the con file. If it does not exist, one will be created
return void
        public void SaveToFile(string FilePath)
        {
            using(FileStream Stream = new FileStream(FilePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read))
            {
                // Clean out the file
                Stream.SetLength(0);

                // Append new data
                using(StreamWriter Writer = new StreamWriter(Stream))
                {
                    foreach (MapListEntry Entry in Entries)
                        Writer.WriteLine(Entry.ToString());
                }
            }
        }

Usage Example

Example #1
0
        /// <summary>
        /// Saves the current generated maplist into the maplist.con file
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SaveBtn_Click(object sender, EventArgs e)
        {
            // Make sure we have at least 1 map :/
            int Len = MapListBox.Lines.Length - 1;

            if (Len == 0 || String.IsNullOrWhiteSpace(MapListBox.Text))
            {
                MessageBox.Show("There must be at least 1 map before saving!", "User Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            // Append the prefix to each map line
            MapList List = MainForm.SelectedMod.MapList;

            // Clear out old Junk, and add new
            List.Entries.Clear();
            for (int i = 0; i < Len; i++)
            {
                List.AddFromString("mapList.append " + MapListBox.Lines[i]);
            }

            // Save and close
            List.SaveToFile(MainForm.SelectedMod.MaplistFilePath);
            this.Close();
        }