Last updated: March 2, 2025

<span aria-hidden="true" id="scan-files-for-viruses-in-net-using-open-source"></span>

# Scan files for viruses in .Net using open source

![Kevin van Zonneveld](/assets/images/teammates/avatar-kvz-4.jpg?dpl=dpl_C6YH6XrtnwbHLJcm1LDKywQn4CB3)

#### Kevin van Zonneveld

Co-founder · Amsterdam, The Netherlands · Show bio

[](https://x.com/kvz)[](https://github.com/kvz)

File security is crucial when your applications handle user-uploaded content. In this DevTip, we'll show how to scan files for viruses in your .NET applications using the open-source antivirus tools nClam and AntiVirusScanner.

<span aria-hidden="true" id="introduction"></span>

## Introduction

Accepting file uploads introduces the risk of malicious content. Integrating virus scanning into your workflow mitigates this risk, safeguarding your users and infrastructure.

<span aria-hidden="true" id="open-source-antivirus-tools-for-net"></span>

## Open-source antivirus tools for .Net

Two effective open-source antivirus tools for .NET are:

* **nClam**: A .NET client for ClamAV, a widely used open-source antivirus engine.
* **AntiVirusScanner**: A library that integrates with Windows Defender and other installed antivirus software.

<span aria-hidden="true" id="set-up-clamav-and-nclam"></span>

## Set up ClamAV and nClam

First, install [ClamAV for Windows⁠](https://www.clamav.net/downloads). After installation, update the virus definitions:

```shell
freshclam

```

Next, install nClam via NuGet:

```shell
dotnet add package nClam

```

<span aria-hidden="true" id="implement-file-scanning-with-nclam-in-c"></span>

## Implement file scanning with nClam in C\#

Here's how to scan a file using nClam:

```csharp
using nClam;

public async Task<bool> IsFileSafeAsync(string filePath)
{
    var clam = new ClamClient("localhost", 3310);
    var scanResult = await clam.SendAndScanFileAsync(filePath);

    switch (scanResult.Result)
    {
        case ClamScanResults.Clean:
            return true;
        case ClamScanResults.VirusDetected:
            Console.WriteLine($"Virus detected: {scanResult.InfectedFiles[0].VirusName}");
            return false;
        default:
            Console.WriteLine("Scan error or unknown result.");
            return false;
    }
}

```

<span aria-hidden="true" id="use-antivirusscanner-for-integration-with-windows-antivirus"></span>

## Use antivirusscanner for integration with Windows antivirus

AntiVirusScanner uses antivirus software already installed on Windows. Install it via NuGet:

```shell
dotnet add package AntiVirusScanner

```

Here's a simple implementation:

```csharp
using AntiVirus;

public bool IsFileSafe(string filePath)
{
    var scanner = new Scanner();
    var result = scanner.ScanAndClean(filePath);

    switch (result)
    {
        case ScanResult.VirusNotFound:
            return true;
        case ScanResult.VirusFound:
            Console.WriteLine("Virus detected!");
            return false;
        default:
            Console.WriteLine("Error during scanning.");
            return false;
    }
}

```

<span aria-hidden="true" id="compare-nclam-and-antivirusscanner"></span>

## Compare nClam and antivirusscanner

|Tool|Pros|Cons|
|-|-|-|
|**nClam**|Cross-platform, detailed control, ClamAV-based|Requires ClamAV setup and maintenance|
|**AntiVirusScanner**|Easy integration, leverages existing antivirus software|Windows-only, limited scanning control|

<span aria-hidden="true" id="best-practices"></span>

## Best practices

* Scan files immediately after upload.
* Regularly update antivirus definitions.
* Handle scanning errors gracefully.
* Log scanning results for auditing and troubleshooting.

<span aria-hidden="true" id="conclusion"></span>

## Conclusion

Integrating virus scanning into your .NET applications significantly enhances security. Both nClam and AntiVirusScanner provide robust solutions, tailored to different needs and environments.

Additional resources:

* [nClam GitHub Repository⁠](https://github.com/tekmaven/nClam)
* [AntiVirusScanner GitHub Repository⁠](https://github.com/AutoItConsulting/AntiVirusScanner)
* [ClamAV Official Documentation⁠](https://docs.clamav.net/)

At Transloadit, we prioritize file security. Our[File Filtering service](/docs/robots/file-virusscan.md) includes a Virus Scan Robot that proactively rejects millions of malicious threats. While we don't currently offer a .NET SDK, you can easily integrate our [REST API](/docs/api.md) directly into your applications.

[#devtips](/devtips.md)#dotnet#security#file-security#open-source-antivirus#file-filtering-service

### 👩‍💻 Join 20k+ developers

Sign up for our [monthly newsletter](/newsletters.md) to receive direct links to 3 exclusive tech — and 2 product updates. No less, no more.

Your email:

Get access

## File uploading and encoding. Made simple.

Transloadit streamlines file handling for developers, trusted by brands like Coursera and The New York Times. We’re known for a reliable API, top-notch support, and a strong commitment to open source, with projects like [Uppy⁠](https://uppy.io) and [Tus⁠](https://tus.io) setting standards in file processing.

[Sign up](/c/)[Book a Demo](https://survey.typeform.com/to/kRg47Xi5)

No credit card needed · 5 GB included in the free plan

Cancel anytime
