I just ran into the same problem and I figured out that you can pass the entire URL into the constructor of SPSite and it will determine which portion of the url is the site collection. Here is my code snippet I used in a method where I am trying to seperate the string into the site collection url and the relative site path.
try
{
//these local variables are created because an out or ref variable cannot be
//accessed inside SPSecuirty.RunWithElevatedPrivileges
string siteCollectionPath = "";
string relativeSitePath = "";
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite spSite = new SPSite(fullPath))
{
relativeSitePath = fullPath.Replace(spSite.Url, "").TrimStart('/');
using (SPWeb spWeb = spSite.AllWebs[relativeSitePath])
{
siteCollectionPath = spSite.Url;
}
}
});
siteCollectionUrl = siteCollectionPath;
relativeSiteUrl = relativeSitePath;
}
catch (FileNotFoundException ex)
{
//the path does not point to a site collection
}
catch (UriFormatException ex)
{
//the path does not point to a site collection
}
catch (ArgumentException ex)
{
//the relativeSitePath was invalid
}
List<Guid>
with the SPWeb.ID, but now I need to do this cross-Site collection and wonder if I can avoid changing the data structure to also have the Site Collection ID.