你的位置:首页 > ASP.net教程

[ASP.net教程]えええ


using Microsoft.SharePoint;using System;using System.Collections.Generic;using System.Configuration;using System.Data;using System.IO;using System.Linq;using System.Net;using System.Text;using System.Threading.Tasks;using System.Web;namespace _20150928{  class Program  {    static void Main(string[] args)    {      //Console.WriteLine(HttpUtility.HtmlDecode("&lt;div class=&quot;ExternalClass261ED404956E49D289332C8D5DE50427&quot;&gt;&lt;p&gt;&lt;a href=&quot;/sites/00/0001/SiteAssets/Lists/Dialogue/AllItems/TCALProjectList.xlsx&quot;&gt;&lt;img width=&quot;16&quot; height=&quot;16&quot; class=&quot;ms-asset-icon ms-rtePosition-4&quot; src=&quot;/_layouts/15/images/icxlsx.png&quot; alt=&quot;&quot; /&gt;TCALProjectList.xlsx&lt;/a&gt;​&lt;/p&gt;&lt;/div&gt;"));      ////string str = "http://baidu.com.cn/hello%20I%27am a apple/te%27st.txt";      ////Console.WriteLine(str.Length);      //Console.ReadKey();      string mainSite = ConfigurationManager.AppSettings["SiteUrl"];      string siteUrl = mainSite + "sites/" + "te" + "/" + "test" + "/";      string listName = "test";      string folderServerRelativeUrl = "/test/test/";      SPFolder folder = CreateFolderInList(siteUrl, listName, folderServerRelativeUrl);      folder = GetSubFolderNewSystem(folder, "test");      SPFolder folderNew = folder;      string strURL = "";//web service get data      string fileName = "";      Stream stream = DownLoadAttachment(strURL);      if (stream != null)      {        MigrationFromFileToFile(folderNew, fileName, stream);      }      string bodystring = "<a href=" + folderNew.Url.Remove(0, mainSite.Length) + fileName + " ><img src=" + "/_layouts/15/images/icxlsx.png" + " />" + fileName + "</a>";    }    public static SPFolder CreateFolderInList(string siteUrl, string listName, string folderServerRelativeUrl)    {      SPFolder returnFolder = null;      using (SPSite site = new SPSite(siteUrl))      using (SPWeb web = site.OpenWeb())      {        SPList list = web.Lists[listName];        SPFolder folder = list.RootFolder;        string[] folderNames = folderServerRelativeUrl.Split('/');        foreach (string folderName in folderNames)        {          if (!string.IsNullOrEmpty(folderName))          {            bool exitFolder = false;            if (folder.SubFolders.Count > 0)            {              foreach (SPFolder subFolder in folder.SubFolders)              {                if (subFolder.Name.Equals(folderName))                {                  exitFolder = true;                  folder = subFolder;                  break;                }              }            }            if (!exitFolder)            {              //フォルダー作成              folder.SubFolders.Add(folderName);              folder = folder.SubFolders[folderName];            }          }        }        returnFolder = folder;      }      return returnFolder;    }    public static SPFolder GetSubFolderNewSystem(SPFolder parentFolder, string folderName)    {      SPFolder subFolder = null;      try      {        subFolder = parentFolder.SubFolders[folderName];        if (!subFolder.Exists)        {          parentFolder.SubFolders.Add(folderName);          subFolder = parentFolder.SubFolders[folderName];        }      }      catch      {        parentFolder.SubFolders.Add(folderName);        subFolder = parentFolder.SubFolders[folderName];      }      return subFolder;    }    public static Stream DownLoadAttachment(string strURL)    {      Stream stream;      try      {        WebClient myWebClient = new WebClient();        myWebClient.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["OldUserName"], ConfigurationManager.AppSettings["OldUserPassword"], ConfigurationManager.AppSettings["OldDomain"]);        byte[] pagedata = myWebClient.DownloadData(strURL);        stream = new MemoryStream(pagedata);        myWebClient.Dispose();        return stream;      }      catch (Exception ex)      {        return null;      }    }    public static void MigrationFromFileToFile(SPFolder folderNew, string fileName, Stream stream)    {      SPFile subFileNew = null;      byte[] contents = StreamToBytes(stream);      if (contents != null)      {        subFileNew = folderNew.Files.Add(folderNew.Url + "/" + fileName, contents, true);      }    }    public static byte[] StreamToBytes(System.IO.Stream stream)    {      int maxsize = Int32.Parse(ConfigurationManager.AppSettings["maxSize"].ToString());      using (System.IO.MemoryStream ms = new System.IO.MemoryStream())      {        byte[] bytes = new byte[maxsize];        int read;        while ((read = stream.Read(bytes, 0, bytes.Length)) > 0)        {          ms.Write(bytes, 0, read);          if (ms.ToArray().Length > maxsize)          {            return null;          }        }        return ms.ToArray();      }    }  }}