title: Creating a Part Uri When Path Contains a #
body: <p>I am zipping a file using the code below. I've found that when a folder in the path being zipped contains a hash (#), CreatePartUri(uri) throws an exception:</p>
  
  <blockquote>
    <p>Part URI cannot contain a Fragment component.</p>
  </blockquote>
  
  <p>Since I cannot alter the folder name, <strong>how can I escape the # symbol in the path so that the Uri will be created correctly?</strong></p>
  
  <pre><code>using System;
  using System.IO;
  using System.IO.Packaging;
  
  namespace ConsoleApplication1
  {
      class Program
      {
          static void Main(string[] args)
          {
              string targetFilePath = "C:\\TEMP.ZIP";
              try
              {
                  if (File.Exists(targetFilePath))
                  {
                      File.Delete(targetFilePath);
                  }
                  string packageRelationshipType = 
                         @"http://schemas.openxmlformats.org/" + 
                         @"package/2007/relationships/htmx/root-html";
  
                 CompressionOption compressionOption = CompressionOption.Maximum;
  
                  using (Package package = Package.Open(targetFilePath,
                                                        FileMode.OpenOrCreate))
                  {
                      string fileName = @"\#TestFolder\TestFile.txt";
                      string filePathOnServer = @"C:\" + fileName;
  
                      Uri uri = new Uri(fileName, UriKind.Relative);
                      Uri partUriDocument = PackUriHelper.CreatePartUri(uri);
  
                      PackagePart packagePartDocument = 
                                    package.CreatePart(partUriDocument, 
                                   System.Net.Mime.MediaTypeNames.Text.RichText,
                                   compressionOption);
  
                      using (FileStream fileStream = new FileStream
                                                        (filePathOnServer, 
                                                         FileMode.Open, 
                                                         FileAccess.Read))
                      {
                         CopyStream(fileStream, packagePartDocument.GetStream());
                      }
  
                      package.CreateRelationship(packagePartDocument.Uri, 
                                                 TargetMode.Internal, 
                                                 packageRelationshipType);
                  }
              }
              catch (Exception e)
              {
                  string exceptionText = e.ToString();
              }
          }
          private static void CopyStream(Stream source, Stream target)
          {
              const int bufSize = 0x1000;
              byte[] buf = new byte[bufSize];
              int bytesRead = 0;
  
              while ((bytesRead = source.Read(buf, 0, bufSize)) &gt; 0)
                  target.Write(buf, 0, bytesRead);
          }
      }
  }
  </code></pre>