Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Deepak RawatRank #646 · 3.7K reputationPosted Jun 26, 2023, 6:02 AM EDT
print numbers in Notepad using C#, you can utilize the
Processclass from theSystem.Diagnosticsnamespace to open Notepad and send the numbers as input. Here's an example:using System; using System.Diagnostics; using System.IO; class Program { static void Main() { string numbers = "1\n2\n3\n4\n5"; // Replace with your numbers // Start the Notepad process Process notepadProcess = Process.Start("notepad.exe"); if (notepadProcess != null) { // Wait for Notepad to be fully opened notepadProcess.WaitForInputIdle(); // Get the main window handle of Notepad IntPtr notepadHandle = notepadProcess.MainWindowHandle; // Set the focus to the Notepad window NativeMethods.SetForegroundWindow(notepadHandle); // Send the numbers to Notepad SendKeys.SendWait(numbers); } else { Console.WriteLine("Failed to start Notepad process."); } } } // NativeMethods class to import the required Windows API functions class NativeMethods { [System.Runtime.InteropServices.DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr hWnd); }In this example, the
numbersvariable holds the sequence of numbers you want to print in Notepad. Each number is separated by a newline character (\n). You can modify thenumbersstring to include your desired numbers or generate it dynamically based on your requirements.The program starts the Notepad process using
Process.Start("notepad.exe"). It waits for Notepad to become idle and then retrieves the main window handle of Notepad. With the help of theSetForegroundWindowfunction from theuser32.dlllibrary, the program sets the focus to the Notepad window. Finally, the numbers are sent to Notepad using theSendKeys.SendWaitmethod.Note: The
SendKeysclass is used to simulate keystrokes, and it requires adding a reference to theSystem.Windows.Formsassembly and importing theSystem.Windows.Formsnamespace. Additionally, make sure to add the necessary error handling and resource cleanup as per your application's requirements.Amit MohantyRank #112 · 20K reputationPosted Jun 27, 2023, 1:57 AM EDT
string notepadPath = @"C:\Windows\System32\notepad.exe"; string filePath = @"D:\textfile.txt"; try { using (StreamWriter streamWriter = new StreamWriter(filePath)) { for (int i = 1; i <= 10; i++) { streamWriter.WriteLine(i); } } Process.Start(notepadPath, filePath); } catch (Exception ex) { Console.WriteLine("An error occurred: " + ex.Message); }Brahma Prakash ShuklaRank #633 · 3.8K reputationPosted Jun 27, 2023, 4:13 AM EDT
using System.Diagnostics; public class NotepadPrinter { public static void PrintNumbers(string numbers) { // Start Notepad process Process notepadProcess = new Process(); notepadProcess.StartInfo.FileName = "notepad.exe"; notepadProcess.StartInfo.UseShellExecute = false; notepadProcess.StartInfo.RedirectStandardInput = true; notepadProcess.Start(); // Write numbers to Notepad notepadProcess.StandardInput.WriteLine(numbers); // Close Notepad notepadProcess.Close(); } } public class Program { public static void Main() { string numbers = "1234567890"; NotepadPrinter.PrintNumbers(numbers); } }