Search code examples
c#firefoxtemporary-files

Send plain text to Firefox


I've got a pretty straight forward question regarding viewing text in Firefox. My application is producing some text that I need to view in Firefox. If I save a .txt file and open that in Firefox, one of the plugins for the browser is able to utilize the text and do what it needs to do.

I've been accomplishing this by creating a temp file, writing the text to it, then opening the file in Firefox. Problem is, I need to delete that file once it's feed to Firefox so I don't have hundreds of these lying around. I'm using the temp file method because I couldn't find info on being able to pass just some straight text in the browser's arguments.

Anyways, this is what I've got right now and you can see my File.Delete actually deletes the file before Firefox can get to it. If i step through the code more slowly, it's fine.

Any ideas?

try
{
   string fileName = Path.GetTempFileName();
   FileInfo fileInfo = new FileInfo(fileName);
   fileInfo.Attributes = FileAttributes.Temporary;

   string writetext = "text I need in a Firefox page";
   File.WriteAllText(fileName, writetext);

   ProcessStartInfo startInfo = new ProcessStartInfo();
   startInfo.FileName = "firefox.EXE";
   startInfo.Arguments = fileName;
   Process.Start(startInfo);

   if (File.Exists(fileName))
     {
         File.Delete(fileName);
     }

   }
   catch (SystemException ex)
   {
     MessageBox.Show("An error occured: " + ex.Message);
   }

Solution

  • Or you can use data URI instead, e.g. firefox.exe "data:text/plain,Lorem ipsum dolor sit amet"