title: C# How to rename a file without using File.Move
body: <p>I have a c# application that connects to a server, gets the datagrid, manipulates each row and then according to each updated row the new row gets uploaded to the server and one file per row gets renamed on the hdd.</p>
  
  <p>The application works totally fine but i analyzed it with the profiler and realised that this line of code:</p>
  
  <pre><code>File.Move(symbolsOldPath, symbolsPath); 
  </code></pre>
  
  <p>takes 80% of the time my application needs to complete its task.</p>
  
  <p>I went through all the questions on StackOverflow and other questions out there if there is a different way for a better performance but i wasnt succesful. The only other way i found was implementing VB to use the Rename method, but as it calls the File.Move method it is no improvement. Do you guys know an alternative way with better performance?</p>
  
  <p>Here is my code of the class that changes the data.</p>
  
  <pre><code>    public DataTable ChangeData(DataTable unchangedData, string searchPathSymbols, string searchPathImages, ProgressBar pbForm)
      {
          pbtemp = pbForm;
          int rowCount = unchangedData.Rows.Count;
  
          foreach (DataRow row in unchangedData.Rows)
          {
              counter++;
  
              if (counter == 10)
              {
                  pbtemp.Value += counter;
                  counter = 0;
                  Application.DoEvents();
              }
  
              number = row[1].ToString();
              symbolsPath = row[2].ToString();
              symbolsPathCopy = symbolsPath;
              imagesPath = row[3].ToString();
              imagesPathCopy = imagesPath;
  
              aliasSymbols = symbolsPath.Substring(0, symbolsPath.IndexOf('&gt;') + 1);
  
              if (symbolsPath == imagesPath)
              {
                  if (aliasSymbols.Contains("Symbole"))
                  {
                      if (!string.IsNullOrEmpty(symbolsPath))
                      {
                          SymbolsChanger(searchPathSymbols, row);
                          row[3] = row[2];
                      }
                  }
  
                  else
                  {
                      if (!string.IsNullOrEmpty(imagesPath))
                      {
                          ImagesChanger(searchPathImages, row);
                          row[2] = row[3];
                      }
                  }
              }
  
              else
              {
                  if (!string.IsNullOrEmpty(symbolsPath))
                  {
                      SymbolsChanger(searchPathSymbols, row);
                  }
  
                  if (!string.IsNullOrEmpty(imagesPath))
                  {
                      ImagesChanger(searchPathImages, row);
                  }
              }
          }
          pbtemp.Value += (rowCount - pbtemp.Value);
          return unchangedData;
      }
  
      private void SymbolsChanger(string searchPathSymbols, DataRow row)
      {
          string symbolsOldPath;
          //Symbols
          //Get and delete Alias and get filepath
  
          int countAliasSymbolsIndex = symbolsPath.LastIndexOf('&gt;') + 1;
          symbolsPath = symbolsPath.Remove(0, countAliasSymbolsIndex);
          symbolsOldPath = searchPathSymbols + "\\" + symbolsPath;
  
          //Remove and replace numbers
          int startSymbolsIndex = 0;
          int endSymbolsIndex = symbolsPath.IndexOf('_') == -1 ? symbolsPath.LastIndexOf('.') : symbolsPath.IndexOf('_');
          int countSymbolsIndex = endSymbolsIndex - startSymbolsIndex;
          symbolsPath = symbolsPath.Remove(startSymbolsIndex, countSymbolsIndex);
          string nameSymbols = number + symbolsPath;
          symbolsPath = searchPathSymbols + "\\" + nameSymbols;
  
          try
          {
              //Rename file
              File.Move(symbolsOldPath, symbolsPath);             
          }
          catch(FileNotFoundException)
          {
              try
              {
                  File.Move(symbolsPath, symbolsPath);
              }
              catch (FileNotFoundException)
              {
                  logArrayDataChange.Add(symbolsPathCopy);
              }
  
          }
  
          row[2] = aliasSymbols + nameSymbols;
      }
  
      private void ImagesChanger(string searchPathImages, DataRow row)
      {
          string imagesOldPath;
          //Images
          //Get and delete Alias and get filepath
          string aliasImage = imagesPath.Substring(0, imagesPath.IndexOf('&gt;') + 1);
          int countAliasImagesIndex = imagesPath.LastIndexOf('&gt;') + 1;
          imagesPath = imagesPath.Remove(0, countAliasImagesIndex);
          imagesOldPath = imagesPath.StartsWith("\\") == true ? searchPathImages + imagesPath : searchPathImages + "\\" + imagesPath;
  
          //Remove and replace numbers
          int startImagesIndex = imagesPath.LastIndexOf("\\") == -1 ? 0 : imagesPath.LastIndexOf("\\");
          int endImagesIndex = imagesPath.IndexOf('_') == -1 ? imagesPath.LastIndexOf('.') : imagesPath.IndexOf('_');
          int countImagesIndex = endImagesIndex - startImagesIndex;
          imagesPath = imagesPath.Remove(startImagesIndex + 1, countImagesIndex - 1);
  
          int insertIndex = imagesPath.LastIndexOf("\\") == -1 ? 0 : imagesPath.LastIndexOf("\\");
          string nameImages = imagesPath.Insert(insertIndex + 1, number);
          imagesPath = searchPathImages + "\\" + nameImages;
  
          try
          {
              //Rename file
              File.Move(imagesOldPath, imagesPath);               
          }
          catch (FileNotFoundException)
          {
              try
              {
                  File.Move(imagesPath, imagesPath);
              }
              catch (FileNotFoundException)
              {
                  logArrayDataChange.Add(imagesPathCopy);
              }
          }
  
          row[3] = aliasImage + nameImages;
      }
  }
  </code></pre>
  
  <p>}</p>