System.IO.FileInfo.MoveTo C# (CSharp) Method

MoveTo() private method

private MoveTo ( String destFileName ) : void
destFileName String
return void
        public void MoveTo(String destFileName)
        {
            if (destFileName == null)
                throw new ArgumentNullException(nameof(destFileName));
            if (destFileName.Length == 0)
                throw new ArgumentException(SR.Argument_EmptyFileName, nameof(destFileName));
            Contract.EndContractBlock();

            String fullDestFileName = Path.GetFullPath(destFileName);

            // These checks are in place to ensure Unix error throwing happens the same way
            // as it does on Windows.These checks can be removed if a solution to #2460 is
            // found that doesn't require validity checks before making an API call.
            if (!new DirectoryInfo(Path.GetDirectoryName(FullName)).Exists)
            {
                throw new DirectoryNotFoundException(SR.Format(SR.IO_PathNotFound_Path, FullName));
            }
            if (!Exists)
            {
                throw new FileNotFoundException(SR.Format(SR.IO_FileNotFound_FileName, FullName), FullName);
            }

            FileSystem.Current.MoveFile(FullPath, fullDestFileName);

            FullPath = fullDestFileName;
            OriginalPath = destFileName;
            _name = Path.GetFileName(fullDestFileName);
            DisplayPath = GetDisplayPath(destFileName);
            // Flush any cached information about the file.
            Invalidate();
        }

Same methods

FileInfo::MoveTo ( string destFileName ) : void

Usage Example

Example #1
3
 public static void Log(string entry)
 {
     if (console_mode)
     {
         Console.WriteLine(entry);
     }
     StreamWriter writer = null;
     try
     {
         FileInfo info = new FileInfo(fileName);
         if (info.Exists && (info.Length >= 0x100000))
         {
             string path = fileName + ".old";
             File.Delete(path);
             info.MoveTo(path);
         }
         writer = new StreamWriter(fileName, true);
         writer.WriteLine("[{0:yyyy-MM-dd HH:mm:ss}] {1}", DateTime.Now, entry);
         writer.Flush();
     }
     catch (Exception)
     {
     }
     finally
     {
         if (writer != null)
         {
             writer.Close();
         }
     }
 }
All Usage Examples Of System.IO.FileInfo::MoveTo