### Install DeviceDetector.NET via NuGet Source: https://context7.com/totpero/devicedetector.net/llms.txt Installs the DeviceDetector.NET library using the .NET CLI or Package Manager Console. This is the first step to using the library in your .NET project. ```bash # Using .NET CLI dotnet add package DeviceDetector.NET # Using Package Manager Console Install-Package DeviceDetector.NET ``` -------------------------------- ### Enhanced Device Detection with HTTP Client Hints Source: https://context7.com/totpero/devicedetector.net/llms.txt Utilizes HTTP Client Hints headers to improve device detection accuracy, especially for modern browsers. This example shows how to parse Client Hints and integrate them with DeviceDetector for more precise results. ```csharp using DeviceDetectorNET; // Simulate HTTP headers from a request (in ASP.NET Core, use Request.Headers) var headers = new Dictionary { { "Sec-CH-UA", "\"Chromium\";v=\"120\", \"Google Chrome\";v=\"120\", \"Not_A Brand\";v=\"24\"" }, { "Sec-CH-UA-Mobile", "?1" }, { "Sec-CH-UA-Platform", "\"Android\"" }, { "Sec-CH-UA-Platform-Version", "\"13.0.0\"" }, { "Sec-CH-UA-Model", "\"SM-G991B\"" }, { "Sec-CH-UA-Full-Version-List", "\"Chromium\";v=\"120.0.6099.144\", \"Google Chrome\";v=\"120.0.6099.144\"" }, { "Sec-CH-UA-Arch", "\"arm\"" }, { "Sec-CH-UA-Bitness", "\"64\"" } }; // Create ClientHints from headers var clientHints = ClientHints.Factory(headers); // Access client hint properties directly Console.WriteLine($"Is Mobile: {clientHints.IsMobile()}"); // True Console.WriteLine($"Platform: {clientHints.GetOperatingSystem()}"); // Android Console.WriteLine($"Version: {clientHints.GetOperatingSystemVersion()}"); // 13.0.0 Console.WriteLine($"Model: {clientHints.GetModel()}"); // SM-G991B Console.WriteLine($"Architecture: {clientHints.GetArchitecture()}"); // arm Console.WriteLine($"Bitness: {clientHints.GetBitness()}"); // 64 // Use with DeviceDetector for enhanced detection var userAgent = "Mozilla/5.0 (Linux; Android 13; SM-G991B) AppleWebKit/537.36"; var dd = new DeviceDetector(userAgent, clientHints); dd.Parse(); // Client hints improve version accuracy var os = dd.GetOs(); Console.WriteLine($"OS: {os.Match.Name} {os.Match.Version}"); // Android 13.0.0 (more precise) ``` -------------------------------- ### Implement In-Memory Caching with DictionaryCache Source: https://context7.com/totpero/devicedetector.net/llms.txt This example shows how to utilize the DictionaryCache class for efficient in-memory caching of parsed regex patterns within the DeviceDetectorNET library. It demonstrates creating a shared cache instance and using it across multiple DeviceDetector objects to improve performance for repeated parsing operations. Key cache operations like Save, Fetch, Contains, Delete, and FlushAll are illustrated. Namespaces DeviceDetectorNET and DeviceDetectorNET.Cache are required. ```csharp using DeviceDetectorNET; using DeviceDetectorNET.Cache; // Create a shared cache instance var cache = new DictionaryCache(); // Use the same cache across multiple DeviceDetector instances var dd1 = new DeviceDetector("Mozilla/5.0 (Windows NT 10.0; Win64; x64)..."); dd1.SetCache(cache); dd1.Parse(); var dd2 = new DeviceDetector("Mozilla/5.0 (iPhone; CPU iPhone OS 17_0)..."); dd2.SetCache(cache); // Reuses cached regex patterns dd2.Parse(); // Cache operations cache.Save("custom_key", "custom_data"); var data = cache.Fetch("custom_key"); var exists = cache.Contains("custom_key"); cache.Delete("custom_key"); cache.FlushAll(); // Clear entire cache ``` -------------------------------- ### Parse Operating System Information with OperatingSystemParser Source: https://context7.com/totpero/devicedetector.net/llms.txt This snippet demonstrates how to use the OperatingSystemParser class to detect operating system details from a user agent string. It includes examples of accessing parsed OS information and utilizing static utility methods for OS family lookups and desktop OS detection. Dependencies include the DeviceDetectorNET.Parser namespace. ```csharp using DeviceDetectorNET.Parser; var userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"; var osParser = new OperatingSystemParser(); osParser.SetUserAgent(userAgent); var result = osParser.Parse(); if (result.Success) { Console.WriteLine($"Name: {result.Match.Name}"); // Windows Console.WriteLine($"Short Name: {result.Match.ShortName}"); // WIN Console.WriteLine($"Version: {result.Match.Version}"); // 10.0 Console.WriteLine($"Platform: {result.Match.Platform}"); // x64 Console.WriteLine($"Family: {result.Match.Family}"); // Windows } // Static utility methods var osFamily = OperatingSystemParser.GetOsFamily("AND"); // Returns "Android" var isDesktop = OperatingSystemParser.IsDesktopOs("WIN"); // Returns true // Get all available operating systems var allOs = OperatingSystemParser.GetAvailableOperatingSystems(); Console.WriteLine($"Supported OS count: {allOs.Count}"); // 200+ // Get OS families var families = OperatingSystemParser.GetAvailableOperatingSystemFamilies(); foreach (var family in families) { Console.WriteLine($"Family: {family.Key}, Members: {string.Join(", ", family.Value)}"); } // Get full OS name from short code var fullName = OperatingSystemParser.GetNameFromId("AND", "13"); // "Android 13" ``` -------------------------------- ### Get User Agent Info with DeviceDetector.NET Source: https://context7.com/totpero/devicedetector.net/llms.txt Parses a user agent string to retrieve comprehensive device, OS, and browser information using a static method. It can also utilize client hints for enhanced accuracy. Returns a result object containing detection success status and matched information. ```csharp using DeviceDetectorNET; var userAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 17_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2 Mobile/15E148 Safari/604.1"; // One-line detection var result = DeviceDetector.GetInfoFromUserAgent(userAgent); if (result.Success) { var info = result.Match; Console.WriteLine($"User Agent: {info.UserAgent}"); Console.WriteLine($"Device Type: {info.DeviceType}"); // smartphone Console.WriteLine($"Device Brand: {info.DeviceBrand}"); // Apple Console.WriteLine($"Device Model: {info.DeviceModel}"); // iPhone Console.WriteLine($"OS Family: {info.OsFamily}"); // iOS Console.WriteLine($"Browser Family: {info.BrowserFamily}"); // Safari if (info.Os != null) { Console.WriteLine($"OS: {info.Os.Name} {info.Os.Version}"); // iOS 17.2 } if (info.Client != null) { Console.WriteLine($"Client: {info.Client.Name} {info.Client.Version}"); // Mobile Safari 17.2 } // Check if it's a bot if (info.Bot != null) { Console.WriteLine($"Bot Detected: {info.Bot.Name}"); } } // With client hints for enhanced detection var headers = new Dictionary { { "Sec-CH-UA-Platform", "\"iOS\"" }, { "Sec-CH-UA-Mobile", "?1" } }; var hints = ClientHints.Factory(headers); var enhancedResult = DeviceDetector.GetInfoFromUserAgent(userAgent, hints); ``` -------------------------------- ### DeviceDetectorSettings - Global Configuration in C# Source: https://context7.com/totpero/devicedetector.net/llms.txt Explains how to configure global settings for DeviceDetector.NET, including specifying custom regex directories, enabling persistent parse caching with LiteDB, and adjusting LRU in-memory cache parameters. Logging can also be configured. ```csharp using DeviceDetectorNET; using System; // Configure before first use (application startup) // Custom regex directory (if not using embedded resources) DeviceDetectorSettings.RegexesDirectory = @"C:\YamlRegexsFiles\"; // Enable persistent parse cache (LiteDB) DeviceDetectorSettings.ParseCacheDBFilename = "DeviceDetectorCache.db"; DeviceDetectorSettings.ParseCacheDBDirectory = @"C:\Cache\"; DeviceDetectorSettings.ParseCacheDBExpiration = TimeSpan.FromDays(365); // Enable with non-zero value // LRU in-memory cache settings DeviceDetectorSettings.LRUCacheMaxSize = 20000; // Max entries DeviceDetectorSettings.LRUCacheCleanPercentage = 30; // Cleanup percentage DeviceDetectorSettings.LRUCacheMaxDuration = TimeSpan.FromMinutes(15); // Entry TTL // Add logging (optional) // DeviceDetectorSettings.Logger = loggerFactory.CreateLogger("DeviceDetector"); ``` -------------------------------- ### Basic Device Detection with DeviceDetector Class Source: https://context7.com/totpero/devicedetector.net/llms.txt Demonstrates the basic usage of the DeviceDetector class to parse a user agent string and retrieve device, operating system, and client information. It includes methods to check device types (mobile, desktop, tablet) and access detailed properties. ```csharp using DeviceDetectorNET; using DeviceDetectorNET.Parser; // Set version truncation globally (optional) DeviceDetector.SetVersionTruncation(VersionTruncation.VERSION_TRUNCATION_NONE); // Create detector with user agent var userAgent = "Mozilla/5.0 (Linux; Android 13; SM-G991B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36"; var dd = new DeviceDetector(userAgent); // Parse the user agent dd.Parse(); // Check device type if (dd.IsMobile()) { Console.WriteLine("This is a mobile device"); } else if (dd.IsDesktop()) { Console.WriteLine("This is a desktop device"); } else if (dd.IsTablet()) { Console.WriteLine("This is a tablet"); } // Get operating system info var os = dd.GetOs(); if (os.Success) { Console.WriteLine($"OS: {os.Match.Name} {os.Match.Version}"); Console.WriteLine($"OS Family: {os.Match.Family}"); Console.WriteLine($"Platform: {os.Match.Platform}"); // Output: OS: Android 13 // Output: OS Family: Android // Output: Platform: ARM } // Get client/browser info var client = dd.GetClient(); if (client.Success) { Console.WriteLine($"Browser: {client.Match.Name} {client.Match.Version}"); Console.WriteLine($"Type: {client.Match.Type}"); // Output: Browser: Chrome Mobile 120.0.0.0 // Output: Type: browser } // Get device info Console.WriteLine($"Device Type: {dd.GetDeviceName()}"); // smartphone Console.WriteLine($"Brand: {dd.GetBrandName()}"); // Samsung Console.WriteLine($"Model: {dd.GetModel()}"); // Galaxy S21 5G ``` -------------------------------- ### LRUCachedDeviceDetector - High-Performance Caching in C# Source: https://context7.com/totpero/devicedetector.net/llms.txt Demonstrates using LRUCachedDeviceDetector for efficient caching of DeviceDetector instances in high-traffic applications. It shows how to retrieve cached instances and configure LRU cache settings like maximum size, cleanup percentage, and time-to-live. ```csharp using DeviceDetectorNET.Cache; // Get a cached DeviceDetector instance (thread-safe) var userAgent = "Mozilla/5.0 (Linux; Android 13) AppleWebKit/537.36"; // First call parses and caches the result var dd = LRUCachedDeviceDetector.GetDeviceDetector(userAgent); // Subsequent calls with the same user agent return cached instance var ddCached = LRUCachedDeviceDetector.GetDeviceDetector(userAgent); // Use the cached detector if (dd != null) { Console.WriteLine($"Is Mobile: {dd.IsMobile()}"); Console.WriteLine($"Device: {dd.GetDeviceName()}"); Console.WriteLine($"Brand: {dd.GetBrandName()}"); } // Configure LRU cache settings (do this at application startup) DeviceDetectorSettings.LRUCacheMaxSize = 50000; // Max cached entries (default: 10000) DeviceDetectorSettings.LRUCacheCleanPercentage = 25; // Purge percentage when full (default: 30) DeviceDetectorSettings.LRUCacheMaxDuration = TimeSpan.FromMinutes(30); // Cache TTL (default: 10 min) ``` -------------------------------- ### Device Type Detection Methods with DeviceDetector.NET Source: https://context7.com/totpero/devicedetector.net/llms.txt Demonstrates how to use specialized methods in DeviceDetector.NET to check for specific device types without parsing the full results. This method is efficient for quickly determining if a user agent corresponds to a smartphone, tablet, desktop, TV, console, or wearable. ```csharp using DeviceDetectorNET; var userAgents = new Dictionary { { "smartphone", "Mozilla/5.0 (Linux; Android 13; Pixel 7) AppleWebKit/537.36 Chrome/120.0 Mobile" }, { "tablet", "Mozilla/5.0 (iPad; CPU OS 17_0 like Mac OS X) AppleWebKit/605.1.15 Safari/604.1" }, { "desktop", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/120.0.0.0" }, { "tv", "Mozilla/5.0 (SMART-TV; Linux; Tizen 6.0) AppleWebKit/537.36 Chrome/85.0" }, { "console", "Mozilla/5.0 (PlayStation; PlayStation 5/1.00) AppleWebKit/605.1.15" }, { "wearable", "Mozilla/5.0 (Linux; Android 11; SM-R870) AppleWebKit/537.36 Chrome/94.0 Mobile" } }; foreach (var (deviceType, ua) in userAgents) { var dd = new DeviceDetector(ua); dd.Parse(); Console.WriteLine($"\n--- {deviceType.ToUpper()} ---"); Console.WriteLine($"User Agent: {ua.Substring(0, 60)}..."); // Specific device type checks Console.WriteLine($"IsMobile: {dd.IsMobile()}"); Console.WriteLine($"IsDesktop: {dd.IsDesktop()}"); Console.WriteLine($"IsTablet: {dd.IsTablet()}"); Console.WriteLine($"IsTv: {dd.IsTv()}"); Console.WriteLine($"IsConsole: {dd.IsConsole()}"); Console.WriteLine($"IsWearable: {dd.IsWearable()}"); Console.WriteLine($"IsTouchEnabled: {dd.IsTouchEnabled()}"); Console.WriteLine($"IsBrowser: {dd.IsBrowser()}"); // Get detected type Console.WriteLine($"Device Name: {dd.GetDeviceName()}"); } ``` -------------------------------- ### Version Truncation Control in C# Source: https://context7.com/totpero/devicedetector.net/llms.txt Illustrates how to control the truncation of version numbers in DeviceDetector.NET's results. This allows for normalizing version data for analytics or focusing on specific parts of the version string (major, minor, patch, build). ```csharp using DeviceDetectorNET; using DeviceDetectorNET.Parser; // VERSION_TRUNCATION_MAJOR: 120 // VERSION_TRUNCATION_MINOR: 120.0 // VERSION_TRUNCATION_PATCH: 120.0.0 // VERSION_TRUNCATION_BUILD: 120.0.0.0 // VERSION_TRUNCATION_NONE: 120.0.6099.144 (full version) // Set globally before parsing DeviceDetector.SetVersionTruncation(VersionTruncation.VERSION_TRUNCATION_NONE); var dd = new DeviceDetector("Mozilla/5.0 Chrome/120.0.6099.144"); dd.Parse(); var client = dd.GetClient(); Console.WriteLine($"Full Version: {client.Match.Version}"); // 120.0.6099.144 // Change to minor version only DeviceDetector.SetVersionTruncation(VersionTruncation.VERSION_TRUNCATION_MINOR); var dd2 = new DeviceDetector("Mozilla/5.0 Chrome/120.0.6099.144"); dd2.Parse(); var client2 = dd2.GetClient(); Console.WriteLine($"Minor Version: {client2.Match.Version}"); // 120.0 ``` -------------------------------- ### Parse User Agent with DeviceDetector.NET Source: https://github.com/totpero/devicedetector.net/blob/master/README.md This snippet demonstrates how to use DeviceDetector.NET to parse a user agent string and extract detailed information about the device, client, OS, brand, and model. It includes optional configurations for version truncation, caching, and bot detection. ```csharp using DeviceDetectorNET; using DeviceDetectorNET.Parser; using System.Linq; using System.Collections.Generic; // OPTIONAL: Set version truncation to none, so full versions will be returned // By default only minor versions will be returned (e.g. X.Y) // for other options see VERSION_TRUNCATION_* constants in DeviceParserAbstract class DeviceDetector.SetVersionTruncation(VersionTruncation.VERSION_TRUNCATION_NONE); var userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"; // Example User-Agent var headers = new Dictionary { { "User-Agent", new[] { userAgent } } }; // Simulate Request Headers var clientHints = ClientHints.Factory(headers); // client hints are optional var dd = new DeviceDetector(userAgent, clientHints); // OPTIONAL: Set caching method // By default static cache is used, which works best within one process (memory array caching) // To cache across requests use caching in files or memcache dd.SetCache(new DictionaryCache()); // OPTIONAL: If called, GetBot() will only return true if a bot was detected (speeds up detection a bit) dd.DiscardBotInformation(); // OPTIONAL: If called, bot detection will completely be skipped (bots will be detected as regular devices then) dd.SkipBotDetection(); dd.Parse(); if(dd.IsBot()) { // handle bots,spiders,crawlers,... var botInfo = dd.GetBot(); Console.WriteLine($"Detected Bot: {botInfo.Name}"); } else { var clientInfo = dd.GetClient(); // holds information about browser, feed reader, media player, ... var osInfo = dd.GetOs(); var device = dd.GetDeviceName(); var brand = dd.GetBrandName(); var model = dd.GetModel(); Console.WriteLine($"Device: {device}"); Console.WriteLine($"Brand: {brand}"); Console.WriteLine($"Model: {model}"); Console.WriteLine($"OS: {osInfo.Name} {osInfo.Version}"); Console.WriteLine($"Client: {clientInfo.Name} {clientInfo.Version}"); } ``` -------------------------------- ### Configure Regexes Directory Path in DeviceDetector.NET Source: https://github.com/totpero/devicedetector.net/blob/master/README.md This snippet demonstrates how to change the default directory path where DeviceDetector.NET looks for its regular expression files. This is useful for custom deployments or when managing regex files separately. ```csharp using DeviceDetectorNET.Configuration; // Set the custom path for the regexes directory DeviceDetectorSettings.RegexesDirectory = @"C:\\CustomRegexsFiles\"; // Now DeviceDetector will load regexes from the specified directory // var dd = new DeviceDetector("Your User Agent"); // dd.Parse(); ``` -------------------------------- ### Detect Browser Information with BrowserParser Source: https://context7.com/totpero/devicedetector.net/llms.txt This code snippet illustrates how to use the BrowserParser class to identify browser details from a user agent string. It covers accessing parsed browser information, including name, version, type, and engine details. Static methods for browser family lookups and mobile-only browser detection are also shown. The required namespace is DeviceDetectorNET.Parser.Client. ```csharp using DeviceDetectorNET.Parser.Client; var userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"; var browserParser = new BrowserParser(userAgent); var result = browserParser.Parse(); if (result.Success) { var browser = result.Match as DeviceDetectorNET.Results.Client.BrowserMatchResult; Console.WriteLine($"Name: {browser.Name}"); // Chrome Console.WriteLine($"Short Name: {browser.ShortName}"); // CH Console.WriteLine($"Version: {browser.Version}"); // 120.0.0.0 Console.WriteLine($"Type: {browser.Type}"); // browser Console.WriteLine($"Engine: {browser.Engine}"); // Blink Console.WriteLine($"Engine Version: {browser.EngineVersion}"); // 120.0.0.0 Console.WriteLine($"Family: {browser.Family}"); // Chrome } // Static utility methods var browserFamily = BrowserParser.GetBrowserFamily("CH"); // Returns "Chrome" var shortName = BrowserParser.GetBrowserShortName("Firefox"); // Returns "FF" var isMobileOnly = BrowserParser.IsMobileOnlyBrowser("OM"); // Opera Mobile - returns true // Get all available browsers var allBrowsers = BrowserParser.GetAvailableBrowsers(); Console.WriteLine($"Supported browsers: {allBrowsers.Count}"); // 700+ // Get browser families var browserFamilies = BrowserParser.GetAvailableBrowserFamilies(); foreach (var family in browserFamilies) { Console.WriteLine($"{family.Key}: {family.Value.Length} browsers"); } ``` -------------------------------- ### Direct Bot Parsing with BotParser in DeviceDetector.NET Source: https://context7.com/totpero/devicedetector.net/llms.txt Utilizes the BotParser class for direct bot detection without the overhead of full device detection. This is efficient when only bot identification is required. It can optionally discard details for faster parsing. ```csharp using DeviceDetectorNET.Parser; var userAgent = "Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)"; var botParser = new BotParser(); botParser.SetUserAgent(userAgent); // Option: discard details for faster parsing botParser.DiscardDetails = false; var result = botParser.Parse(); if (result.Success) { Console.WriteLine($"Bot: {result.Match.Name}"); // bingbot Console.WriteLine($"Category: {result.Match.Category}"); // Search bot Console.WriteLine($"URL: {result.Match.Url}"); // http://www.bing.com/bingbot.htm if (result.Match.Producer != null) { Console.WriteLine($"Producer: {result.Match.Producer.Name}"); // Microsoft Corporation } } else { Console.WriteLine("Not a bot - proceed with regular device detection"); } ``` -------------------------------- ### ASP.NET Core Integration with DeviceDetector.NET Source: https://context7.com/totpero/devicedetector.net/llms.txt Integrates DeviceDetector.NET into ASP.NET Core applications to automatically detect devices from incoming requests. It utilizes request headers and client hints for enhanced accuracy. The output includes detailed information about the detected user agent, device, OS, and client. ```csharp using DeviceDetectorNET; using DeviceDetectorNET.Parser; using Microsoft.AspNetCore.Mvc; [ApiController] [Route("[controller]")] public class DeviceInfoController : ControllerBase { [HttpGet] public IActionResult Get() { // Set version truncation for full versions DeviceDetector.SetVersionTruncation(VersionTruncation.VERSION_TRUNCATION_NONE); // Get user agent from request var userAgent = Request.Headers.UserAgent.ToString(); // Build client hints from request headers var headers = Request.Headers.ToDictionary( h => h.Key, h => h.Value.FirstOrDefault() ?? string.Empty ); var clientHints = ClientHints.Factory(headers); // Parse using static method var result = DeviceDetector.GetInfoFromUserAgent(userAgent, clientHints); if (!result.Success) { return Ok(new { detected = false }); } var info = result.Match; return Ok( new { detected = true, userAgent = info.UserAgent, device = new { type = info.DeviceType, brand = info.DeviceBrand, model = info.DeviceModel }, os = info.Os != null ? new { name = info.Os.Name, version = info.Os.Version, family = info.OsFamily } : null, client = info.Client != null ? new { name = info.Client.Name, version = info.Client.Version, type = info.Client.Type } : null, isBot = info.Bot != null, bot = info.Bot != null ? new { name = info.Bot.Name, category = info.Bot.Category } : null } ); } } ``` -------------------------------- ### Detect Bots with DeviceDetector.NET Source: https://context7.com/totpero/devicedetector.net/llms.txt Identifies bots, crawlers, and spiders from user agent strings. This method allows for full bot detail retrieval or faster detection by discarding details or skipping bot detection entirely. It handles hundreds of known bots. ```csharp using DeviceDetectorNET; // Googlebot user agent var botUserAgent = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"; var dd = new DeviceDetector(botUserAgent); dd.Parse(); if (dd.IsBot()) { var bot = dd.GetBot(); if (bot.Success) { Console.WriteLine($"Bot Name: {bot.Match.Name}"); // Googlebot Console.WriteLine($"Category: {bot.Match.Category}"); // Search bot Console.WriteLine($"URL: {bot.Match.Url}"); // http://www.google.com/bot.html Console.WriteLine($"Producer: {bot.Match.Producer?.Name}"); // Google Inc. } } // Discard bot information for faster detection (only returns true/false) var fastDetector = new DeviceDetector(botUserAgent); fastDetector.DiscardBotInformation(); fastDetector.Parse(); if (fastDetector.IsBot()) { Console.WriteLine("Bot detected (details discarded for speed)"); } // Skip bot detection entirely (treats bots as regular devices) var skipBotDetector = new DeviceDetector(botUserAgent); skipBotDetector.SkipBotDetection(); skipBotDetector.Parse(); Console.WriteLine($"IsBot: {skipBotDetector.IsBot()}"); // false (bot detection skipped) // Now you can detect browser/OS even for bot user agents var client = skipBotDetector.GetClient(); ``` -------------------------------- ### Parse User Agent specifically for Bots with DeviceDetector.NET Source: https://github.com/totpero/devicedetector.net/blob/master/README.md This snippet shows how to use the dedicated BotParser from DeviceDetector.NET to efficiently check if a user agent string belongs to a known bot, spider, or crawler. It's optimized for scenarios where only bot detection is needed. ```csharp using DeviceDetectorNET.Parser; var userAgent = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"; // Example Bot User-Agent var botParser = new BotParser(); botParser.SetUserAgent(userAgent); // OPTIONAL: discard bot information. Parse() will then return true instead of information botParser.DiscardDetails = true; var result = botParser.Parse(); if (result != null) { // A bot was detected Console.WriteLine("Bot detected!"); // If DiscardDetails is false, you can access bot details: // var botInfo = result; // Console.WriteLine($"Bot Name: {botInfo.Name}"); } else { // handle non-bot requests Console.WriteLine("Not a bot."); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.