Why C# Is Not Just About Websites and Games?
\n\nWhen you hear "automation," most people think of Python or Bash. And that's fair: they are lightweight, script-based, "for the poor." But what if you are a .NET developer who doesn't want to learn a new syntax for a single script? Or your project is already in C#, and dragging in Python dependencies just to parse logs is a nightmare?
\n\nWelcome to the world of task automation in C#. This isn't just about "writing a console application." It's a full-fledged tool that can work with the file system, registry, COM objects, GUI, and even execute PowerShell commands. And all of this — with strong typing, IntelliSense, and no surprises.
\n\nIn this article, we'll break down 5 specific scenarios you can apply today. No fluff — just code and real-world cases.
\n\n1. Working with Files: Cleaning Logs, Archiving Projects
\n\nThe most common pain for any developer is bloated log folders, junk files, and the need to back up builds. C# automation of file operations is done in 5 lines.
\n\nExample: Deleting Files Older Than N Days
\n\nusing System.IO;\n\nstring path = @"C:\\Logs\\";\nint daysOld = 30;\n\nvar oldFiles = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories)\n .Where(f => File.GetLastWriteTime(f) < DateTime.Now.AddDays(-daysOld));\n\nforeach (var file in oldFiles)\n{\n File.Delete(file);\n Console.WriteLine($"Deleted: {file}");\n}\n\n\nChange SearchOption.AllDirectories to TopDirectoryOnly — and the script works only in the root. Add try-catch — and it's ready for production.
Example: Archiving a Folder with a Date
\n\nusing System.IO.Compression;\n\nstring source = @"C:\\Projects\\MyApp\\bin\\Release";\nstring zipPath = $@"C:\\Backups\\MyApp_{DateTime.Now:yyyy-MM-dd}.zip";\n\nZipFile.CreateFromDirectory(source, zipPath);\nConsole.WriteLine($"Archive created: {zipPath}");\n\n\nThis code can be hooked up to Windows Task Scheduler — and you can forget about manual backups. Automation in C# in action.
\n\n2. Working with the Registry: Changing System Settings
\n\nSometimes you need to write a registry key for all users or, conversely, delete a malicious entry. C# via Microsoft.Win32 does this safely and without BAT files.
using Microsoft.Win32;\n\n// Setting a value\nRegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\\MyCompany", true);\nif (key == null)\n{\n key = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\\MyCompany");\n}\nkey.SetValue("InstallPath", @"C:\\Program Files\\MyApp");\nkey.Close();\n\n\nFor HKEY_CURRENT_USER, use Registry.CurrentUser. Important: HKLM requires administrator rights — run the application as administrator.
3. PowerShell from C#: Bridging Two Worlds
\n\nSometimes it's easier to write a PowerShell command than to implement it in C#. For example, getting a list of installed programs or restarting a service. The System.Management.Automation library allows you to perform scripting in C# by calling PS commands.
Install the NuGet package:
\nInstall-Package Microsoft.PowerShell.SDK\n\nExample: Restarting a Service
\n\nusing System.Management.Automation;\n\nusing (PowerShell ps = PowerShell.Create())\n{\n ps.AddScript("Restart-Service -Name 'Spooler' -Force");\n var results = ps.Invoke();\n \n if (ps.HadErrors)\n {\n foreach (var error in ps.Streams.Error)\n {\n Console.WriteLine($"Error: {error.Exception.Message}");\n }\n }\n else\n {\n Console.WriteLine("Service restarted.");\n }\n}\n\n\nThis way you get the flexibility of PowerShell and the safety of C#. Perfect for Windows automation in cor