title: Could not find a part of the path in c#
body: <p>I am trying to build a method which is able to write in a file the number of files in another folder. I already got it but it does not work because gives me an error:</p>
  
  <blockquote>
    <p>Could not find a part of the path '
    C:\Users\Administrator\Desktop...\bin\release\%USERPROFILE%\AppData\Local_logC'</p>
  </blockquote>
  
  <p>This is my code: </p>
  
  <pre><code>private static int GetCountGFiles(string dirname)
  {
      int fileCount = 0;
      dirname = dirname.TrimEnd(new char[] { '\\' }) + @"\";
  
      var fixedDirname = Environment.ExpandEnvironmentVariables(dirname);
  
      fileCount += System.IO.Directory.GetFiles(fixedDirname).Length;
  
      string[] all_subdirs = System.IO.Directory.GetDirectories(dirname);
  
      foreach (string dir in all_subdirs)
          fileCount += GetCountGFiles(dirname); 
  
      string data = string.Format("LogReg.txt");
  
      System.IO.Directory.CreateDirectory(filePath);
      var fileandpath = filePath + data;
  
      using (StreamWriter writer = File.AppendText(fileandpath))
      {
          writer.WriteLine("[" + DateTime.Now + "]" + " - " + "Nº de Records: (" + fileCount.ToString() + ")");
      }
  
      return fileCount;
  }
  </code></pre>
  
  <p>And then I call the method like this : </p>
  
  <pre><code>GetCountGFiles(@"%USERPROFILE%\AppData\Local\Cargas - Amostras\_logsC\");
  </code></pre>
  
  <p>What should I do?</p>