CopyCode The Code is as follows: // <summary>
/// Generate a Google website 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 ");
// Scan all files on the website
Showfiles (rootdirectory, writer );
Writer. writeendelement ();
Writer. writeenddocument ();
Writer. Close ();
Return true;
}
Catch (exception ERR)
{
Return false;
}
}
// Scan all files on the website
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. receivettings ["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: regular, hourly: hour, daily: Day, weekly: Week, monthly: Month, yearly: Year
Writer. writeendelement ();
Writer. writestartelement ("Priority ");
Writer. writestring ("0.8"); // priority relative to other pages. The value is between 0.0 and 1.0.
Writer. writeendelement ();
Writer. writeendelement ();
}
Foreach (directoryinfo D in Dir. getdirectories ())
{
Showfiles (D. fullname, writer );
}
}
Catch (exception ){}
}