fCraft.Paths.MoveOrReplace C# (CSharp) Method

MoveOrReplace() public static method

public static MoveOrReplace ( [ source, [ destination ) : void
source [
destination [
return void
        public static void MoveOrReplace( [NotNull] string source, [NotNull] string destination )
        {
            if ( source == null )
                throw new ArgumentNullException( "source" );
            if ( destination == null )
                throw new ArgumentNullException( "destination" );
            if ( File.Exists( destination ) ) {
                if ( Path.GetPathRoot( Path.GetFullPath( source ) ) == Path.GetPathRoot( Path.GetFullPath( destination ) ) ) {
                    string backupFileName = destination + ".bak";
                    File.Replace( source, destination, backupFileName, true );
                    File.Delete( backupFileName );
                } else {
                    File.Copy( source, destination, true );
                }
            } else {
                File.Move( source, destination );
            }
        }

Usage Example

Example #1
0
        public static void Save()
        {
            CheckIfLoaded();
            const string tempFileName = Paths.PlayerDBFileName + ".temp";

            lock ( SaveLoadLocker ) {
                PlayerInfo[] listCopy = PlayerInfoList;
                Stopwatch    sw       = Stopwatch.StartNew();
                using (FileStream fs = OpenWrite(tempFileName)) {
                    using (StreamWriter writer = new StreamWriter(fs, Encoding.UTF8, BufferSize)) {
                        writer.WriteLine("{0} {1} {2}", maxID, FormatVersion, Header);

                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < listCopy.Length; i++)
                        {
                            listCopy[i].Serialize(sb);
                            writer.WriteLine(sb.ToString());
                            sb.Length = 0;
                        }
                    }
                }
                sw.Stop();
                Logger.Log(LogType.Debug,
                           "PlayerDB.Save: Saved player database ({0} records) in {1}ms",
                           Trie.Count, sw.ElapsedMilliseconds);

                try {
                    Paths.MoveOrReplace(tempFileName, Paths.PlayerDBFileName);
                } catch (Exception ex) {
                    Logger.Log(LogType.Error,
                               "PlayerDB.Save: An error occured while trying to save PlayerDB: {0}", ex);
                }
            }
        }
All Usage Examples Of fCraft.Paths::MoveOrReplace