Ok, the only solution I see is quite a hack:
- Write the data on a file;
- Open the file with the default app using
Process.Start
;
- Delete the file with
File.Delete
.
Code:
// test data that I'll write on the file
var text = Enumerable.Range(0, 1000000).Select(x => x.ToString()).ToArray();
// choose the Desktop folder to verify that
// the file is deleted at the end of the method
var tempDir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
// choose a random file name that will be unique 99.999999999% of the times
var filePath = Path.Combine(tempDir, Guid.NewGuid().ToString() + ".txt");
// write the file. Here I use WriteAllLines, you can use StreamWriter
File.WriteAllLines(filePath, text);
// start the default app for this path
Process.Start(filePath);
// wait to let the default app to open the file;
// otherwise the app will crash, not finding the file anymore
// just in the middle of the read
// (I put 2 sec, but the time must be verified
// depending on your system and the file size)
Thread.Sleep(2000);
// this will totally delete the file
File.Delete(filePath);
If you have Notepad as default app for txt files, that's what you'll see: Notepad opens up with your data, but the file doesn't exist anymore. That's quite what you wanted, isn't it? You won't find the file in the Recycle Bin neither, so you won't have disk space leaks.
The only defect of this trick is: if you click "Save" on your app, it won't ask you the path where you want to save the file. Instead, it will simply re-create the file as it was before deletion, and will save the data directly. That's because it opened a physical file, it didn't created a new one, so it remembers the filePath and will use it to save.
If you don't find more correct/professional solutions, this one can do its job.
ASIDE:
I'd suggest to you a little refactoring.
First step, avoid repetitions:
using (var writer = new StreamWriter("file.txt"))
{
var array = grr[0].Keys.ToArray();
writer.WriteLine(array[0] + "," + array[1] + "," + array[2] + "," + array[3]);
for (int r = 0; r < row - 1; r++)
{
var grrr = grr[r].Values.ToArray();
writer.WriteLine(grrr[0] + "," + grrr[1] + "," + grrr[2] + "," + grrr[3]);
}
}
Second step, use more advanced built-in functions:
using (var writer = new StreamWriter("file.txt"))
{
writer.WriteLine(string.Join(",", grr[0].Keys.ToArray()));
for (int r = 0; r < row - 1; r++)
{
writer.WriteLine(string.Join(",", grr[r].Values.ToArray()));
}
}