文件夾的新增

using System;

using System.Collections;

using System.Collections.Generic;

using System.Text;

using System.IO;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Utilities;

using System.Data;

using System.Text;

namespace UploadFolders

{

   class Program

   {

       static void Main(string[] args)

       {

           int i;

           string searchpattern = "*";

           string dir = @"dirpath";

           ArrayList arraylist = new ArrayList();

           ArrayList array_List = new ArrayList();

           DirectoryInfo info = new DirectoryInfo(dir);

           SPSite site = new SPSite("siteurl");

           SPWeb web = site.OpenWeb();

           SPList list = web.Lists["Upload Documents"];

           string listurl = "listurl";

           GetFiles(info, searchpattern, ref arraylist);

           try

           {

               for (i = 0; i < arraylist.Count; i++)

               {

                   string FileName = arraylistIdea.ToString();

                   FolderStructure(FileName, ref array_List);

                   string fname = FileName.Substring(FileName.LastIndexOf('\\') + 1);

                   string foldername = arraylistIdea.ToString();

                   SPQuery query = new SPQuery();

                   string fieldname = list.Fields["Name"].InternalName;

                   query.Query = "<Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='Text'>" + foldername + "</Value></Eq></Where>"; ;

                   SPListItemCollection items = list.GetItems(query);

                   if (items.Count == 0)

                   {

                       GetFolder(web, list, foldername);

                   }

               }

           }

           catch (Exception ex)

           {

               Console.WriteLine(ex.Message);

               Console.ReadLine();

           }

       }

       //Function to get all the folders in the specified path.

       public static void GetFiles(DirectoryInfo info, string searchpattern, ref ArrayList arraylist)

       {

           foreach (FileInfo finfo in info.GetFiles(searchpattern))

           {

               arraylist.Add(finfo.FullName);

           }

           foreach (DirectoryInfo dirInfo in info.GetDirectories())

           {

               FileAttributes fa=dirInfo.Attributes;

               GetFiles(dirInfo, searchpattern, ref arraylist);

           }

       }

       //Function to get the FolderStructure in the specified path.

       public static int FolderStructure(string path, ref ArrayList array_List)

       {

           if (string.IsNullOrEmpty(path))

               return 0;

           DirectoryInfo parent = Directory.GetParent(path);

           if (parent == null)

               return 1;

           else

           {

               FolderStructure(parent.FullName, ref array_List);

               return array_List.Add(parent);

           }

       }

       internal static SPFolder GetFolder(SPWeb web, SPList list, string foldername)

       {

           if (string.IsNullOrEmpty(foldername))

               return list.RootFolder;

           SPFolder folder = web.GetFolder(list.RootFolder.Url + "/" + foldername);

           if (!folder.Exists)

           {

               if (!list.EnableFolderCreation)

               {

                   list.EnableFolderCreation = true;

                   list.Update();

               }

               // We couldn't find the folder so create it

               string[] folders = foldername.Trim('\\').Split('\\');

               string folderPath = string.Empty;

               for (int i = 1; i < folders.Length-1; i++)

               {

                   folderPath += "/" + foldersIdea;

                   folder = web.GetFolder(list.RootFolder.Url + folderPath);

                   if (!folder.Exists)

                   {

                       SPListItem newFolder = list.Items.Add("", SPFileSystemObjectType.Folder, folderPath.Trim('/'));

                       web.AllowUnsafeUpdates = true;

                       newFolder.Update();

                       web.AllowUnsafeUpdates = false;

                       folder = newFolder.Folder;

                   }

                   //Adding the item to a sharepoint library.

                   if (i == folders.Length - 2)

                   {

                       string destinationurl = folder.ToString() + "/" + folders[folders.Length-1].ToString();

                       web.AllowUnsafeUpdates = true;

                       FileStream fs = File.Open(foldername, FileMode.Open);

                       web.Files.Add(destinationurl, fs, true);

                       fs.Close();

                       web.AllowUnsafeUpdates = false;

                       Console.WriteLine("Added file : " + folders[folders.Length - 1].ToString());

                   }

               }

           }

           // Still no folder so error out

           if (folder == null)

               throw new SPException(string.Format("The folder '{0}' could not be found.", foldername));

           return folder;

       }

   }

}

posted @ 2009-07-07 11:03  小生不才  阅读(231)  评论(0)    收藏  举报