本来指望MOSS自带上传文件夹下所有子文件和子文件夹的命令行,查了很久都没有找到,没办法只好写代码调用MOSS的API上传文件了,如下是代码样例:
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.IO;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Configuration;
namespace MOSSUpload
{
/// <summary>
/// MOSS文档操作的类
/// </summary>
public class FileOpr
{
/// <summary>
/// 上传文件夹及其下所有的内容到MOSS
/// </summary>
/// <param name="fileDir">文件夹全路径,如D:\test</param>
/// <param name="mossUrl">MOSS的地址</param>
/// <param name="mossFolderUrl">上传到MOSS的文件夹的地址</param>
/// <param name="dtLastUpload">上次上传时间</param>
public static void UploadMossFile(string fileDir,string mossUrl,string mossFolderUrl,DateTime dtLastUpload)
{
SPSite spSite = new SPSite(mossUrl);
SPWeb spWeb = spSite.OpenWeb();
try
{
// 获取要上传的MOSS文件夹
SPFolder fold = spWeb.GetFolder(mossFolderUrl);
if (fold.Exists)
{
ListFiles(new DirectoryInfo(fileDir), spWeb, fold.ServerRelativeUrl, dtLastUpload);
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
spWeb.Close();
spSite.Close();
}
}
/// <summary>
/// 遍历文件夹和文件,逐步上传MOSS
/// </summary>
/// <param name="info">文件对象</param>
/// <param name="spWeb">SPWeb对象</param>
/// <param name="parentFolderUrl">MOSS上父文件夹路径</param>
/// <param name="dtLastUpload">上次更新时间,放在config文件中,如果文件在该时间前已经上传过,就不传了</param>
public static void ListFiles(FileSystemInfo info, SPWeb spWeb, string parentFolderUrl, DateTime dtLastUpload)
{
if (!info.Exists) return;
try
{
string strFileName = string.Empty;
DirectoryInfo dir = info as DirectoryInfo;
// 不是目录
if (dir == null) return;
FileSystemInfo[] files = dir.GetFileSystemInfos();
for (int i = 0; i < files.Length; i++)
{
FileInfo file = files[i] as FileInfo;
// 是文件
if (file != null)
{
// 如果VSS文件的最后更新日期小于最后上传MOSS时间,则不上传文件
if (DateTime.Compare(file.LastWriteTime,dtLastUpload) <= 0)
{
continue;
}
//Console.WriteLine("文件:" + file.Name + "\t" + file.Length);
// 把文件转化为字节流
FileStream fs = new FileStream(file.FullName, FileMode.Open);
byte[] content = new byte[fs.Length];
fs.Read(content, 0, (int)fs.Length);
fs.Close();
// 去掉文件名中的特殊字符
strFileName = Regex.Replace(file.Name, @"[#%&*:<>?/{|}]", "");
// 查看文件是否已经存在,如果存在则签出
SPFile tmpFile = spWeb.GetFile(parentFolderUrl + "/" + strFileName);
if (tmpFile.Exists)
{
tmpFile.CheckOut();
}
spWeb.Files.Add(parentFolderUrl + "/" + strFileName, content, true);
if (tmpFile.CheckOutStatus != SPFile.SPCheckOutStatus.None)
{
tmpFile.CheckIn("自动上传文档签入", SPCheckinType.OverwriteCheckIn);
}
}
else
{
// 对于子目录,在MOSS建立文件夹,然后进行递归调用
// 获取要上传的文件夹
string folderUrl = parentFolderUrl + "/" + Regex.Replace(files[i].Name, @"[#%&*:<>?/{|}]", "");
SPFolder folder = spWeb.GetFolder(folderUrl);
// 如果文件夹不存在则新建
if (!folder.Exists)
{
folder = spWeb.Folders.Add(folderUrl);
}
ListFiles(files[i], spWeb, folderUrl, dtLastUpload);
}
}
}
catch (Exception ex)
{
WriteLog("上传文件出错:" + ex.Message);
}
}
/// <summary>
/// 简单记录log日志
/// </summary>
/// <param name="strLogMgr">日志内容</param>
public static void WriteLog(string strLogMgr)
{
// log日志文件名
string strFileName = ConfigurationManager.AppSettings["LogPath"];
// 如果日志文件不存在则创建文件
if (!File.Exists(strFileName))
{
FileStream fs = File.Create(strFileName);
fs.Close();
}
// 写入Log日志
using (StreamWriter sw = File.AppendText(strFileName))
{
sw.WriteLine("------------Time:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
sw.WriteLine(strLogMgr);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.IO;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Configuration;
namespace MOSSUpload
{
/// <summary>
/// MOSS文档操作的类
/// </summary>
public class FileOpr
{
/// <summary>
/// 上传文件夹及其下所有的内容到MOSS
/// </summary>
/// <param name="fileDir">文件夹全路径,如D:\test</param>
/// <param name="mossUrl">MOSS的地址</param>
/// <param name="mossFolderUrl">上传到MOSS的文件夹的地址</param>
/// <param name="dtLastUpload">上次上传时间</param>
public static void UploadMossFile(string fileDir,string mossUrl,string mossFolderUrl,DateTime dtLastUpload)
{
SPSite spSite = new SPSite(mossUrl);
SPWeb spWeb = spSite.OpenWeb();
try
{
// 获取要上传的MOSS文件夹
SPFolder fold = spWeb.GetFolder(mossFolderUrl);
if (fold.Exists)
{
ListFiles(new DirectoryInfo(fileDir), spWeb, fold.ServerRelativeUrl, dtLastUpload);
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
spWeb.Close();
spSite.Close();
}
}
/// <summary>
/// 遍历文件夹和文件,逐步上传MOSS
/// </summary>
/// <param name="info">文件对象</param>
/// <param name="spWeb">SPWeb对象</param>
/// <param name="parentFolderUrl">MOSS上父文件夹路径</param>
/// <param name="dtLastUpload">上次更新时间,放在config文件中,如果文件在该时间前已经上传过,就不传了</param>
public static void ListFiles(FileSystemInfo info, SPWeb spWeb, string parentFolderUrl, DateTime dtLastUpload)
{
if (!info.Exists) return;
try
{
string strFileName = string.Empty;
DirectoryInfo dir = info as DirectoryInfo;
// 不是目录
if (dir == null) return;
FileSystemInfo[] files = dir.GetFileSystemInfos();
for (int i = 0; i < files.Length; i++)
{
FileInfo file = files[i] as FileInfo;
// 是文件
if (file != null)
{
// 如果VSS文件的最后更新日期小于最后上传MOSS时间,则不上传文件
if (DateTime.Compare(file.LastWriteTime,dtLastUpload) <= 0)
{
continue;
}
//Console.WriteLine("文件:" + file.Name + "\t" + file.Length);
// 把文件转化为字节流
FileStream fs = new FileStream(file.FullName, FileMode.Open);
byte[] content = new byte[fs.Length];
fs.Read(content, 0, (int)fs.Length);
fs.Close();
// 去掉文件名中的特殊字符
strFileName = Regex.Replace(file.Name, @"[#%&*:<>?/{|}]", "");
// 查看文件是否已经存在,如果存在则签出
SPFile tmpFile = spWeb.GetFile(parentFolderUrl + "/" + strFileName);
if (tmpFile.Exists)
{
tmpFile.CheckOut();
}
spWeb.Files.Add(parentFolderUrl + "/" + strFileName, content, true);
if (tmpFile.CheckOutStatus != SPFile.SPCheckOutStatus.None)
{
tmpFile.CheckIn("自动上传文档签入", SPCheckinType.OverwriteCheckIn);
}
}
else
{
// 对于子目录,在MOSS建立文件夹,然后进行递归调用
// 获取要上传的文件夹
string folderUrl = parentFolderUrl + "/" + Regex.Replace(files[i].Name, @"[#%&*:<>?/{|}]", "");
SPFolder folder = spWeb.GetFolder(folderUrl);
// 如果文件夹不存在则新建
if (!folder.Exists)
{
folder = spWeb.Folders.Add(folderUrl);
}
ListFiles(files[i], spWeb, folderUrl, dtLastUpload);
}
}
}
catch (Exception ex)
{
WriteLog("上传文件出错:" + ex.Message);
}
}
/// <summary>
/// 简单记录log日志
/// </summary>
/// <param name="strLogMgr">日志内容</param>
public static void WriteLog(string strLogMgr)
{
// log日志文件名
string strFileName = ConfigurationManager.AppSettings["LogPath"];
// 如果日志文件不存在则创建文件
if (!File.Exists(strFileName))
{
FileStream fs = File.Create(strFileName);
fs.Close();
}
// 写入Log日志
using (StreamWriter sw = File.AppendText(strFileName))
{
sw.WriteLine("------------Time:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
sw.WriteLine(strLogMgr);
}
}
}
}