Masking a CSV file through shuffling

I wrote a bit on data masking here. Below is a simple C# program to reshuffle data in a csv file. It uses FileHelpers library for the .Net Framework.

To play with this simple code, first download FileHelpers library. Then save the text below the C# code into a file, modify the code to so it points to the text file you just saved. And it should work. Nothing fancy, just a proof of concept kind of work.

[sourcecode language=”c#”]
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using FileHelpers;

namespace FileTest
class Program
static Random randomNumber = new Random();
static void Main(string[] args)

FileHelperEngine engine = new FileHelperEngine(typeof(People));
engine.Options.IgnoreEmptyLines = true;
People[] res = engine.ReadFile(@”c:\junk\PeopleName.txt”) as People[];
for (int i = 0; i < res.Length; i++) { //Console.WriteLine(GenerateRandomString(9) + Console.WriteLine(res[randomNumber.Next(res.Length)].FirstName + res[randomNumber.Next(res.Length)].LastName); } } public static long CountLinesInFile(string fileName) { long count = 0; using (StreamReader reader = new StreamReader(fileName)) { string line; while ((line = reader.ReadLine()) != null) { count++; } } return count; } public static string GenerateRandomString(int intLenghtOfString) { //Create a new StrinBuilder that would hold the random string. StringBuilder randomString = new StringBuilder(); //Create a new instance of the class Random //Create a variable to hold the generated charater. Char appendedChar; //Create a loop that would iterate from 0 to the specified value of intLenghtOfString for (int i = 0; i <= randomNumber.Next(1, intLenghtOfString); ++i) { //Generate the char and assign it to appendedChar appendedChar = Convert.ToChar(Convert.ToInt32(26 * randomNumber.NextDouble()) + 97); //Append appendedChar to randomString randomString.Append(appendedChar); } //Convert randomString to String and return the result. return randomString.ToString(); } [FixedLengthRecord(FixedMode.AllowLessChars)] class People { [FieldFixedLength(9)] public string FirstName; [FieldFixedLength(14)] public string LastName; [FieldFixedLength(27)] public string Address1; [FieldFixedLength(18)] public string City; } } } [/sourcecode]

Robert   Foster         123 Roosevolt Ave.         Chicago
Edward Smith 93874 Madison Oak Park
James Kane 324 Grant Geneva
Mei Liu 587 Lincoln River Forest
Jane Doe 6565 Truman Maywood
Peter Imanov 456 Wilson Berkeley
Daming Wang 897 Jackson Aurora
Spiro Agnew 233 Jefferson Naperville
Osama BinLaden 6431 Kennedy Arlington Heights
George Bush 2356 Grant Evanston
Dick Cheney 6498 Coolidge Skokie
Barack Obama 8439 Adams Glenview


Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.