title: c# - obtain the filename of base64 encoded file
body: <p>Im trying to obtain the name of a infopath 2007 attachment file (inside an xml, which is encoded by default in base64Binary), i have achieved this before with the same code but for some reason i cant obtain the original file name, of the  attachments inside xml files im using now (which i need to find the attachment's .extension and the file also needs to have the same name as before), however if i write the file extension manually i can export the file, so my question is: why can i write the file from the base64 string successfully and im not able to get its name.<br>
  <strong>Notes:</strong><br></p>
  
  <ul>
  <li>im currently trying to decode images, im not sure if the problems might be here, however as mentioned if i know the .ext and type it manually i can decode it</li>
  <li>the xml file from where im getting the attachment encoded string was 'exported'(for lack of a better word, saved?) from infopath 2007</li>
  <li>the <code>decodeFromBase64</code> works fine</li>
  <li><code>getFileName</code> worked on diffrent 'pack' of xml i used before, as far as i know there arent any relevant diffrences because im able to obtain the encoded string and decode the file</li>
  <li>im using code extremely simillar to that provided in the decoding class, by microsoft: <a href="https://support.microsoft.com/en-us/help/892730/how-to-encode-and-decode-a-file-attachment-programmatically-by-using-visual-c-in-infopath-2003" rel="nofollow noreferrer">https://support.microsoft.com/en-us/help/892730/how-to-encode-and-decode-a-file-attachment-programmatically-by-using-visual-c-in-infopath-2003</a></li>
  </ul>
  
  <p>code to write the file (that works):</p>
  
  <pre><code>public static void decodeFromBase64(string encodedString)
  {
      using (FileStream stream = System.IO.File.Create("D:\\some dir\\file.png"))
      {
          byte[] byteArray = Convert.FromBase64String(file);
          stream.Write(byteArray, 0, byteArray.Length);
      }
  }
  </code></pre>
  
  <p>code to get the file name, that throws: 'System.ArgumentOutOfRangeException' <strong>here:</strong>
   /**********************************************/</p>
  
  <pre><code>byte[] _fileNameBytes = _theReader.ReadBytes(_attachmentNameLength);
  </code></pre>
  
  <p>/**********************************************/</p>
  
  <pre><code>  public static string getFileName(string attachedFile)
      {
          string fname;
          byte[] thdata = Convert.FromBase64String(attachedFile);
  
              Encoding _encoding = Encoding.Unicode;
              using (MemoryStream _memoryStream = new MemoryStream(thdata))
              {
  
                  BinaryReader _theReader = new BinaryReader(_memoryStream);
                  _theReader.BaseStream.Position = 0;
                  byte[] _headerData = _theReader.ReadBytes(16);
  
                  int _fileSize = (int)_theReader.ReadUInt32();
                  int _attachmentNameLength = (int)_theReader.ReadUInt32() * 2;
  
                  byte[] _fileNameBytes = _theReader.ReadBytes(_attachmentNameLength);
                  fname = _encoding.GetString(_fileNameBytes, 0, _attachmentNameLength - 2);
                  if (fname.Length &gt; 0)
                      return fname;
              }
          return fname;
      }
  </code></pre>
  
  <p><strong>edit:</strong><br></p>
  
  <ul>
  <li>i forgot to mention that using the same code awhile ago i tried to decode a string encoded from the previous files and it worked</li>
  </ul>
  
  <p><strong>edit2:</strong><br>
  <strong>############################## SOLVED #################################</strong><br>
  Even though i found the solution im not sure of the <strong>exact</strong> source of the problem. The integrety of my encoded string was somehow corrupted (somehow i messed up the construction of my test templates, after testing the xml files where the attachments were, everything went great <strong>ALSO</strong> the only files whose encoded strings didnt work so far were my own, which makes total sense considering i never used infopath directly before)<br>
  <strong>For future reference</strong>: here are official decoder and encoder classes<br></p>
  
  <ul>
  <li>InfoPath 2003<br>
  support.microsoft _com/en-us/help/892730/how-to-encode-and-decode-a-file-attachment-programmatically-by-using-visual-c-in-infopath-2003<br></li>
  <li>InfoPath 2007 and 2010<br>
  support.microsoft _com/en-us/help/2517906/how-to-encode-and-to-decode-a-file-attachment-programmatically-by-using-visual-c-in-infopath-2010-or-in-infopath-2007<br><br>
  <strong><em>(cant post more than 2 links, i'll edit it later)</em></strong></li>
  </ul>