How to manage the Master Page and Layouts List on SharePoint
Here is an easy way to manage the _catalogs/masterpages folder.
using (SPSite site = new SPSite(url))
using (SPWeb web = site.OpenWeb())
SPFolder fld = web.Folders["_catalogs"];
SPFolder mPagesFolder = fld.SubFolders["_catalogs/masterpage"];
foreach (SPFile mpage in mPagesFolder.Files)