Copy Code code as follows:
<summary>
Create a Google site map
</summary>
<returns></returns>
public static Boolbuildgooglesitemap ()
{
Try
{
string rootdirectory = AppDomain.CurrentDomain.BaseDirectory;
XmlTextWriter Writer = new XmlTextWriter (HttpContext.Current.Server.MapPath ("~/googlesitemaps.xml"), Encoding.GetEncoding ("Utf-8"));
writer.formatting = formatting.indented;
Writer.writestartdocument ();
Writer.writestartelement ("Urlset", "http://www.google.com/schemas/sitemap/0.84");
Traverse all files on the scan site
Showfiles (RootDirectory, Writer);
Writer.writeendelement ();
Writer.writeenddocument ();
Writer.close ();
return true;
}
catch (Exception err)
{
return false;
}
}
Traverse all files on the scan site
static void Showfiles (String dirpath, XmlTextWriter Writer)
{
bool Isread = true;
String[] Notread ={"App_Data", "Bin", "FCKeditor", "JS", "MyAdmin", "Powerchatroom"};//exclude these folders
foreach (string s in Notread)
{
String dirname = Dirpath. Substring (Dirpath. LastIndexOf (@ "\") + 1);
if (dirname = s)
{
Isread = false;
Break
}
}
if (! Isread)
Return
Try
{
DirectoryInfo dir = new DirectoryInfo (Dirpath);
foreach (FileInfo f in dir.) GetFiles ())
{
String path = dir. Fullname.replace (AppDomain.CurrentDomain.BaseDirectory, "");//File relative directory
HttpContext.Current.Response.Write (AppDomain.CurrentDomain.BaseDirectory + "**********" + dir.) FullName + "<br>");
Writer.writestartelement ("url");
Writer.writestartelement ("Loc");
StringBuilder sb = new StringBuilder ("/" + path + "/" + f.name);
Sb. Replace ("//", "/"). Replace (@ "\", "/");
Writer.writestring (configurationmanager.appsettings["Websiteurl"). ToString () + sb. ToString ());
Writer.writeendelement ();
Writer.writestartelement ("Lastmod");
Writer.writestring (String. Format ("{0:yyyy-mm-dd}", F.lastwritetime));
Writer.writeendelement ();
Writer.writestartelement ("Changefreq");
Writer.writestring ("always");/Update frequency: Always: Often, hourly: hours, daily: Days, Weekly: Weeks, Monthly: months, yearly: Years
Writer.writeendelement ();
Writer.writestartelement ("priority");
Writer.writestring ("0.8");//precedence over other pages, this value is set between 0.0-1.0
Writer.writeendelement ();
Writer.writeendelement ();
}
foreach (DirectoryInfo d in dir.) GetDirectories ())
{
Showfiles (D.fullname, Writer);
}
}
catch (Exception) {}
}