MOSS 文档库操作类

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
using System.IO;

namespace StatutesPublication
{
public class DocLib
{
private string siteUri;//网站集
private string webUri;//网站
private string docLibUri;//文档库
SPList list = null;

public DocLib(string siteUri, string webUri, string docLibUri)
{
this.siteUri = siteUri;
this.webUri = webUri;
this.docLibUri = docLibUri;
}

//打开文档库并返回其引用
public SPList Open()
{
SPSite site = null;
SPWeb web = null;
SPList list = null;
//打开网站集
try
{
site = new SPSite(siteUri);
}
catch (Exception ex)
{
throw new Exception("网站集错误:" + ex.Message);
}
//打开文档库所在网站
try
{
web = site.OpenWeb(webUri);
}
catch (Exception ex)
{
throw new Exception("网站错误:" + ex.Message);
}
//打开文档库
try
{
web.Lists.IncludeRootFolder = true;//SPList.RootFolder的访问默认没有开启
list = web.Lists[docLibUri];
}
catch (Exception ex)
{
throw new Exception("文档库错误:" + ex.Message);
}
return list;
}

public string Upload(string filePath,string dirName,string fileName)
{
try
{
list = this.Open();
}
catch
{
return "文档库打开错误!";
}
SPFolder rootFolder = list.RootFolder;
if (dirName == "root")//直接保存在根目录
{
if(FileExists("root",fileName))
rootFolder.Files.Delete(rootFolder.Url + "/" + fileName);
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] content = new byte[fs.Length];
fs.Read(content, 0, (int)fs.Length);
rootFolder.Files.Add(fileName, content);
fs.Close();
}
else
{
SPFolder subFolder = null;
subFolder = rootFolder.SubFolders.Add(rootFolder.Url + "/" + dirName);
subFolder = rootFolder.SubFolders[dirName];
if(FileExists(dirName,fileName))
subFolder.Files.Delete(subFolder.Url + "/" + fileName);
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] content = new byte[fs.Length];
fs.Read(content, 0, (int)fs.Length);
subFolder.Files.Add(fileName, content);
fs.Close();
}
return "Success";
}

public bool FileExists(string dirName, string fileName)
{
try
{
list = this.Open();
}
catch
{
return false;
}
SPFolder rootFolder = list.RootFolder;
if (dirName == "root")
{
foreach (SPFile file in rootFolder.Files)
{
if (file.Name == fileName)
return true;
}
}
else
{
SPFolder subFolder = null;
subFolder = rootFolder.SubFolders.Add(rootFolder.Url + "/" + dirName);
subFolder = rootFolder.SubFolders[dirName];
foreach (SPFile file in subFolder.Files)
{
if (file.Name == fileName)
return true;
}
}
return false;
}

public void FileRemove(string dirName, string fileName)
{
try
{
list = this.Open();
}
catch
{
return;
}
SPFolder rootFolder = list.RootFolder;
if (dirName == "root")
{
foreach (SPFile file in rootFolder.Files)
{
if (file.Name == fileName)
file.Delete();
}
}
else
{
SPFolder subFolder = null;
subFolder = rootFolder.SubFolders.Add(rootFolder.Url + "/" + dirName);
subFolder = rootFolder.SubFolders[dirName];
foreach (SPFile file in subFolder.Files)
{
if (file.Name == fileName)
file.Delete();
}
}
}

//读取用户
public string GetUser()
{
SPSite site = null;
SPWeb web = null;
//打开网站集
try
{
site = new SPSite(siteUri);
}
catch (Exception ex)
{
//throw new Exception("网站集错误:" + ex.Message);
return String.Empty;
}
//打开文档库所在网站
try
{
web = site.OpenWeb(webUri);
}
catch (Exception ex)
{
//throw new Exception("网站错误:" + ex.Message);
return String.Empty;
}
string spUser = web.CurrentUser.Name;
return spUser;
}
}
}

你可能感兴趣的:(OS)