1

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);
   }
ikathegreat
  • 2,311
  • 9
  • 49
  • 80
  • You need to wait till the process has fired up. Maybe the following thread will be useful: [Making a Process.Start wait until the process has started-up](http://stackoverflow.com/questions/6390030/c-sharp-making-a-process-start-wait-until-the-process-has-start-up) – astro boy Jun 13 '12 at 00:51

3 Answers3

4

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

Ilya Kozhevnikov
  • 10,242
  • 4
  • 40
  • 70
  • +1. Thanks for this info - I'd never seen this before, and was surprised to learn it works as is in an open Firefox instance (without the quotes) as well. – Ken White Jun 13 '12 at 01:02
  • 1
    Quotes are for when I tried to launch it from command line, otherwise string is treated as array of parameters and Fx would open tabs for every word. – Ilya Kozhevnikov Jun 13 '12 at 01:04
  • Best part about data uris is to base64 some html/javascript and embed it into a badly tested limited-formatting-support site for some simple xss. – Ilya Kozhevnikov Jun 13 '12 at 01:06
  • Yes, I'm aware of what the quotes are for; that's why I mentioned not needing them in the active FF window. As far as "best part", I don't find xss exploits the "best part" of anything. – Ken White Jun 13 '12 at 01:13
  • I'm a QA. Finding them is fun. – Ilya Kozhevnikov Jun 13 '12 at 01:15
0

I would create sub folder specific to app in user TEMP folder and store files there.

Doing that I would assume that nobody else will write to that folder but me.

Every time Process.Start code is run all files need to be removed from that folder using filter "file was created more than 30 min ago" (value can be different).

In worst case scenario there will be a few files in that folder but amount of files will not grow.

Dmitry Harnitski
  • 5,838
  • 1
  • 28
  • 43
0

There a couple of approaches you could take here. The riskiest approach is to just call Thread.Sleep( 5000 ) and just hope that that's enough time for Firefox to read the file before you go and delete it. However, on a very slow system, that might not be enough.

The better approach is to wait until the process exits before deleting the file:

var p = Process.Start( startInfo );
p.WaitForExit();
if( File.Exists( fileName ) ) {
    File.Delete( fileName );
}

This, of course, will block your calling program until Firefox exits. There's another option still, and that's to wait for the Exited event:

var p = Process.Start( startInfo );
p.Exited += FirefoxExited;

// ...

void FirefoxExited( object sender, EventArgs e ) {
    if( File.Exists( fileName ) )
        File.Delete( fileName );
}
Ethan Brown
  • 26,892
  • 4
  • 80
  • 92