0

I need to create a .txt file in C# .

Is there a way (and how to do?) to create a file and open it in Notepad, but without saving in somewhere first? The user would save it after he checks it.

Vy Do
  • 46,709
  • 59
  • 215
  • 313
user2516578
  • 19
  • 2
  • 10
  • 3
    There's no such thing as a text file without saving. Without saving, all you have is text. – ProgrammingLlama Apr 10 '20 at 08:36
  • 1
    Does this answer your question? [How to open text in Notepad from .NET?](https://stackoverflow.com/questions/7613576/how-to-open-text-in-notepad-from-net) – kapsiR Apr 10 '20 at 08:37
  • Save the file, open it with notepad, wait for input idle and then delete the file. the user has to specify a new file name because the opened file does not exist anymore – Oguz Ozgul Apr 10 '20 at 08:45
  • notepad.exe does not put lock on the files, just loads them into memory – Oguz Ozgul Apr 10 '20 at 08:45
  • 1
    Yu can open notepad emtpy and send the text there.. – TaW Apr 10 '20 at 08:53
  • Is this something you want? https://stackoverflow.com/a/41783520/4860100 – OhmnioX Apr 10 '20 at 10:56
  • Here's [the solution](https://learn.microsoft.com/en-us/dotnet/visual-basic/developing-apps/programming/computer-resources/how-to-start-an-application-and-send-it-keystrokes) @TaW proposed (although it's in Visual Basic and uses the `My.` libraries) But it's easy enough to [use `My` from C#](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/namespaces/how-to-use-the-my-namespace) too. – Wyck Nov 08 '20 at 05:20

2 Answers2

1

No not really, i've seen some programs that do this like so, but its not ideal:

  1. Create a temporary file, most programs use the temp directory you get there by using %temp% or C:\Users\{username}\AppData\Local\Temp so e.g. File.Create(@"C:\Users\{username}\AppData\Local\Temp\myTempFile.Txt")
  2. Open the file with notepad. (File.Open(@"C:\Users\{username}\AppData\Local\Temp\myTempFile.Txt")) The user makes the change and saves
  3. Your program checks the file to see if any edits were made.
  4. if any edits have been made, you can prompt the user to save the file to the actual location. e.g. !string.IsNullOrWhiteSpace(File.ReadAllText(@"C:\Users\{username}\AppData\Local\Temp\myTempFile.Txt"))
  5. If the user wants to save the file, the file gets copied to the real location File.Copy(@"C:\Users\{username}\AppData\Local\Temp\myTempFile.Txt", @"c:\myRealPath\MyRealFileName.txt"
sommmen
  • 6,570
  • 2
  • 30
  • 51
0

I created a way (I don't know if it already exists), using System.Diagnostics and System.Reflection libraries. My code is:

File.WriteAllText($@"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\exampleDoc.txt", "information inside file");
while (!File.Exists($@"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\exampleDoc.txt")) { }
Process.Start($@"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\exampleDoc.txt");

while (Process.GetProcesses().Where(prc => { try { return prc.MainWindowTitle.Contains("exampleDoc.txt"); } catch { return false; } }).Count() == 0) { }
File.Delete($@"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\exampleDoc.txt");
David Buck
  • 3,752
  • 35
  • 31
  • 35