title: Create a new file for a known shell type in windows using C#
body: <p>Windows shell allows to associate a file type with a certain application through its extension. This association often provides <code>ShellNew</code> command, which allows to create a new file of a given type with a default template provided by an application. When you select <code>New</code> in context menu of windows explorer it offers a selection of different file types, which can be created this way. I want to implement this action from my C# application.</p>
  <p>It is trivial to open an existing file as follows.</p>
  <pre><code>System.Diagnostics.ProcessStartInfo psi = 
    new System.Diagnostics.ProcessStartInfo(path);
  psi.UseShellExecute = true;
  System.Diagnostics.Process.Start(psi);
  </code></pre>
  <p>If you pass any valid path of an existing file with an extension registered for shell &quot;Open&quot; command, it will open a given file in its default application.
  For instance, pass <code>path = &quot;text.txt&quot;</code> and Notepad will open &quot;text.txt&quot; for you.</p>
  <p>However, I cannot find a code to create new file in a similar manner.</p>
  <p>For instance, Microsoft Word is a registered application for &quot;New&quot; command. I can create new MS Word document with a default template directly from windows explorer without opening MS Word. I want to do exactly same from my C# application: create new MS Word file without linking to MS Word automation.</p>
  <p>I m sure that it is possible to do for any file type properly registered for &quot;New&quot; command in windows shell. But strangely, this topic is not covered in all standard searches I ve tried.</p>
  <h2>Proposed solution</h2>
  <p>After despairing to find any ready code snippet, I've composed this function to extract the info from registry. It gets a file extension from requested full path to new file. Then it finds associated class info for given extension and extracts &quot;FileName&quot; value from &quot;ShellNew&quot; subkey. Finally, it copies a template from the path found in registry to desired destingation of new file.</p>
  <pre><code>public static bool CreateFileOfKnownType(string path)
  {
    try
    {
      string extension = System.IO.Path.GetExtension(path);
      if (string.IsNullOrEmpty(extension)) return false;
  
      using (var rExt = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(extension))
      {
        string[] apps = rExt.GetSubKeyNames();
        foreach (string app in apps)
        {
          if (app.Contains(&quot;.&quot;))
          {
            string shellNew = apps[0] + &quot;\\ShellNew&quot;;
            using (Microsoft.Win32.RegistryKey rApp = rExt.OpenSubKey(shellNew))
            {
              string newFileTemplate = rApp.GetValue(&quot;FileName&quot;).ToString();
              System.IO.File.Copy(newFileTemplate, path);
              return true;
            }
          }
        }
      }
      return false;
    }
    catch { return false; }
  }
  </code></pre>
  <p>This code works fine in some cases when &quot;FileName&quot; contains full path to application template. However, in other cases &quot;FileName&quot; contains only file name of the template without full path. Then this code fails. If somebody knows an improvement, which will work universally, it will be very appreciated.</p>