我有一个关于在 Firefox 中查看文本的非常直接的问题。我的应用程序正在生成一些我需要在 Firefox 中查看的文本。如果我保存一个 .txt 文件并在 Firefox 中打开它,浏览器的一个插件就能够利用该文本并执行它需要执行的操作。
我一直通过创建一个临时文件,向其中写入文本,然后在 Firefox 中打开该文件来完成此操作。问题是,我需要在将该文件提供给 Firefox 后将其删除,这样我就没有数百个这样的文件了。我正在使用临时文件方法,因为我找不到有关能够在浏览器参数中传递一些直接文本的信息。
无论如何,这就是我现在得到的,你可以看到我的 File.Delete 实际上在 Firefox 可以访问它之前删除了文件。如果我更慢地逐步执行代码,那很好。
有什么想法吗?
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);
}
请您参考如下方法:
或者您可以使用 data URI相反,例如firefox.exe "data:text/plain,Lorem ipsum dolor sit amet"
