Task Automation in C#: 5 Powerful Tools for the Lazy Developer

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Why C# Is Not Just About Websites and Games?

\n\n

When 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\n

Welcome 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\n

In this article, we'll break down 5 specific scenarios you can apply today. No fluff — just code and real-world cases.

\n\n

1. Working with Files: Cleaning Logs, Archiving Projects

\n\n

The 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\n

Example: Deleting Files Older Than N Days

\n\n
using 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\n

Change SearchOption.AllDirectories to TopDirectoryOnly — and the script works only in the root. Add try-catch — and it's ready for production.

\n\n

Example: Archiving a Folder with a Date

\n\n
using 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\n

This code can be hooked up to Windows Task Scheduler — and you can forget about manual backups. Automation in C# in action.

\n\n

2. Working with the Registry: Changing System Settings

\n\n

Sometimes 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.

\n\n
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\n

For HKEY_CURRENT_USER, use Registry.CurrentUser. Important: HKLM requires administrator rights — run the application as administrator.

\n\n

3. PowerShell from C#: Bridging Two Worlds

\n\n

Sometimes 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.

\n\n

Install the NuGet package:

\n
Install-Package Microsoft.PowerShell.SDK
\n\n

Example: Restarting a Service

\n\n
using 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\n

This way you get the flexibility of PowerShell and the safety of C#. Perfect for Windows automation in cor

Blogs

Book Recommendations