Check out the latest documentation.

What's Covered

This tutorial illustrates how to append a CSV file containing User-Agent strings with IsMobile, PlatformName and PlatformVersion properties. The following aspects of the API are covered:

  • How to perform a User-Agent match.
  • How to reuse resources to perform subsequent matching.
  • How to retrieve match results for a specific property.
  • How to append a property value to a CSV file.

Code and Explanation

Offline processing example of using 51Degrees device detection. The example shows how to:
  1. Set the various settings for the 51Degrees detector

    											
    string fileName = args[0];
    DataSet dataSet = StreamFactory.Create(fileName, false);
    string[] properties = {"IsMobile","PlatformName","PlatformVersion"};
    
    											
  2. Instantiate the 51Degrees device detection provider with these settings

    											
    Provider provider = new Provider(dataSet);
    
    											
  3. Open an input file with a list of User-Agents, and an output file,

    											
    StreamReader fin = new StreamReader(inputFile);
    StreamWriter fout = new StreamWriter(outputFile);
    
    											
  4. Write a header to the output file with the property names in '|' separated CSV format ('|' separated because some User-Agents contain commas)

    											
    fout.Write("User-Agent");
    for (i = 0; i < properties.Count(); i++ )
    {
        fout.Write("|" + properties[i]);
    }
    fout.Write("\n");
    
    											
  5. For the first 20 User-Agents in the input file, perform a match then write the User-Agent along with the values for chosen properties to the CSV.

    											
    while ((userAgent = fin.ReadLine()) != null &&
                currentLine < linesToRead)
    {
        provider.Match(userAgent, match);
        fout.Write(userAgent);
        foreach (var property in properties)
        {
            fout.Write("|" + match[property]);
        }
        fout.Write("\n");
        currentLine++;
    }
    
    											
This tutorial assumes you are building this from within the 51Degrees Visual Studio solution. Running the executable produced inside Visual Studio will ensure all the command line arguments are preset correctly. If you are running outside of Visual Studio, make sure to add the path to a 51Degrees data file and "20000 User Agents.csv" as arguments.

Full Source File
											
        public static void Run(string fileName, string inputFile)
        {
            // DataSet is the object used to interact with the data file.
            // StreamFactory creates Dataset with pool of binary readers to 
            // perform device lookup using file on disk.
            DataSet dataSet = StreamFactory.Create(fileName, false);

            // Provides access to device detection functions.
            Provider provider = new Provider(dataSet);

            // Used to store and access detection results.
            // Same match will be reused multiple times to avoid unnecessary 
            // object creation.
            Match match = provider.CreateMatch();;

            // Name of the output file results will be saved to.
            string outputFile = "OfflineProcessingOutput.csv";

            // How many lines of the input CSV file to read.
            const int linesToRead = 20;

            // Line currently being read.
            int currentLine = 0;

            // HTTP User-Agent string to match, taken from input the CSV file.
            string userAgent;

            // 51Degrees properties to append the CSV file with.
            // For the full list of properties see:
            // https://51degrees.com/resources/property-dictionary
            string[] properties = { "IsMobile", "PlatformName", "PlatformVersion" };

            // Opens input and output files.
            StreamReader fin = new StreamReader(inputFile);
            StreamWriter fout = new StreamWriter(outputFile);

            Console.WriteLine("Starting Offline Processing Example.");

            // Print CSV headers to output file.
            fout.Write("User-Agent");
            for (int i = 0; i < properties.Count(); i++)
            {
                fout.Write("|" + properties[i]);
            }
            fout.Write("\n");

            while ((userAgent = fin.ReadLine()) != null &&
                     currentLine < linesToRead)
            {
                // Match current User-Agent. Match object reused.
                provider.Match(userAgent, match);
                // Write the User-Agent.
                fout.Write(userAgent);
                // Append properties.
                foreach (var property in properties)
                {
                    fout.Write("|" + match[property]);
                }
                fout.Write("\n");
                currentLine++;
            }

            fin.Close();
            fout.Close();

            Console.WriteLine("Output Written to " + outputFile);

            // Finally close the dataset, releasing resources and file locks.
            dataSet.Dispose();
        }


											
Full Source File

Summary

Offline device detection is frequently required for a variety of reasons such as generating reports. The example is based on an actual support request where several properties had to be added to the CSV file before it could be passed on for another department to use.

This tutorial covered how to use the detector offline to append the first 20 lines of a CSV file with Lite properties: IsMobile , PlatformName and PlatformVersion . Using a Premium or an Enterprise data file gives you access to a far greater number of properties including HardwareVendor , PriceBand , ScreenInchesWidth , IsCrawler and more. A full list of properties and the data file version they are present in can be viewed in the Property Dictionary .