Saving objects to the registry

by OfirYaron 4/27/2009 7:49:00 AM

I've needed a way to save a complex object to the registry so I've wrote this class,

basically it uses reflection in order to find the properties of a generic type that was given and fill them in with the matching (by name) key on the registry.

public class RegistrySmartAgent

{

public string BasePath { get; private set; }public RegistrySmartAgent(string basePath)

{

BasePath = basePath;

}

private T ReadSingle<T>(string fullPath) where T : new()

{

RegistryKey key = Registry.LocalMachine.OpenSubKey(fullPath);

T t = new T();

foreach (string name in key.GetValueNames())

{

Type typeInfo = typeof(T);

PropertyInfo propertyInfo = typeInfo.GetProperty(name);

if ((propertyInfo != null) &&

(propertyInfo.PropertyType == typeof(int)) ||(propertyInfo.PropertyType ==

typeof(string)) ||

(propertyInfo.PropertyType == typeof(double)))propertyInfo.SetValue(t, key.GetValue(name),

null);

}

return t;

}

public IDictionary<string, T> Read<T>(string subName) where T : new()

{

RegistryKey key = Registry.LocalMachine.OpenSubKey(BasePath + "\\" + subName);

IDictionary<string,T> retList = new Dictionary<string,T>();

foreach (string name in key.GetSubKeyNames())

{

retList.Add(name,ReadSingle<T>(BasePath +
"\\" + subName + "\\" + name));

}

return retList;

}

public bool Write<T>(string subName, T element)

{

Type typeInfo = typeof(T);

PropertyInfo[] properties = typeInfo.GetProperties();

foreach (PropertyInfo propertyInfo in properties)

{

RegistryKey key = Registry.LocalMachine.OpenSubKey(BasePath + "\\" + subName);

if (key==null)key =

Registry.LocalMachine.CreateSubKey(BasePath + "\\" + subName);key.SetValue(propertyInfo.Name, propertyInfo.GetValue(element, null));

}

return true;

}

}

Currently rated 4.5 by 2 people

  • Currently 4.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

Sanitize YouTube embed objects

by OfirYaron 4/20/2009 5:56:00 AM

It's contributing alot to a blog or a site when one allows embedding youTube videos to the posts or comments,

the real down-side is that by doing so you have to take into considiration the breach of cross side scripts that the object tag allows.

so for that matter I wrote a Regular expression that validate the input and retrive the URL from within the embed tag, then I placed it on a template tag that I know is safe,
By doing so you can even control the panel color and size that best suit you site.

here is how it works:

private string SanitizeYouTubeObject(string input)

{

//You Tube object Regular Expression

Regex youTubeObjExp =

new Regex(

@"(<embed\ssrc=""http://www.youtube.com(?<Url>[\w\d\s\p{L}\p{S}:#@%/~_?\+-=\\\.&’]+(?=""))([\w\d\s\p{L}\p{S}:""#@%/~_?\+-=\\\.&’]+)(></embed>))",

RegexOptions.IgnoreCase);

if (youTubeObjExp.IsMatch(input))

{

Match match = youTubeObjExp.Match(input);

string videoObject = string.Format(

@"<object width=""480"" height=""385""><param name=""movie"" value=""{0}""></param><param name=""allowFullScreen"" value=""true""></param><param name=""allowscriptaccess"" value=""always""></param><embed src=""{0}"" type=""application/x-shockwave-flash"" allowscriptaccess=""always"" allowfullscreen=""true"" width=""480"" height=""385""></embed></object>", "http://www.youtube.com" + match.Groups["Url"].Value);

//Return the sanitized Tag

return videoObject;

}

//In case it wasn't a legit Object Tag

return null;

}

 

its a simple example only to pass the idea.

A real generic solution will take a dictionary of known embed regexs (youTube, MySpace, etc...) and sanitize any given input accordinly, that way you allow a closed list of well know sites and the DB doesn't have to store the entire tag, the URL will be enough.

Currently rated 4.5 by 4 people

  • Currently 4.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

Powered by BlogEngine.NET 1.3.0.0
Theme by Mads Kristensen

About the author

Name of author Author name
Something about me and what I do.

E-mail me Send mail

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

Recent posts

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

Sign in