简体   繁体   中英

Create and open temporary text file with C# Winforms

I have a winforms app that collects and compiles data and displays on a form. I want a user to be able to click a button that essentially exports that data to a txt file where they can print or save the text file.

I know about using StreamWriter txtFile = File.CreateText(@"C:\\path\\Foo.txt") and I've used it for generating error logs and stuff, but that may not work because I don't necessarily want to create a new permanent file. Basically, what I'd like is:

User clicks a button, and then Notepad pops up with some info (based off of stuff stored in variables). The user can then decide if they want to save it or print it or whatever. When they close it, they'll be prompted to save (like Notepad always does) and then if they choose not to save, the file just disappears. If I use that above code, I'll end up with a new file created everytime a user clicks that button, which I don't want cuz then my drives will be full of 10,000 text files.

E : Okay, so a couple of people have suggested using this solution:

SendMessage to Notepad++ in C#

But that isn't working for me. I copied the code, dropped in a button, changed the Notepad++ to just Notepad and it's not working. I added in the appropriate using lines. Still not working. I'm not saying that this isn't the solution, I'm just saying that I can't get it to work.

First Option: I would suggest to show user a save dialog.

Read Documentation at: https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-save-files-using-the-savefiledialog-component

   // Displays a SaveFileDialog so the user can save the Image  
   // assigned to Button2.  
   SaveFileDialog saveFileDialog1 = new SaveFileDialog();  
   saveFileDialog1.Filter = "Text Image|*.txt";  
   saveFileDialog1.Title = "Save the File";  
   saveFileDialog1.ShowDialog();  

   // If the file name is not an empty string open it for saving.  
   if(saveFileDialog1.FileName != "")  
   {   
       /// Write a text file here at path saveFileDialog1.FileName
   }

Second Option (if you still want to stick to temp files):

I would suggest to use temp folder.

   string windowsTempPath = Path.GetTempPath();

   var filePath = Path.Combine(windowsTempPath, "file_" + DateTime.Now.ToFileTimeUtc() + ".txt");

   //// Write txt file

   //// For opening file, use Process.Start
   Process.Start(filePath);

This would launch default program to open the txt file. You will not have any dependency on notepad or notepad++.

Hope this helps.

You can do the following, using the clipboard and managing 'notepad.exe' process (it should work for any text editor):

1) Add this reference:

    [DllImport("user32.dll")]
    static extern bool SetForegroundWindow(IntPtr hWnd);

2) Open notepad and paste your data:

public void OpenNotepad()
{
    Clipboard.SetText("All your text");

    Process process = Process.Start("notepad.exe");
    process.WaitForInputIdle();
    SetForegroundWindow(process.MainWindowHandle);
    SendKeys.Send("^V");
}

Beware that this will not create any temporary file, but I assume that is the behavior you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM