• 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;
    }
    }
    }

  • 相关阅读:
    [可能没有默认的字体]Warning: imagettfbbox() [function.imagettfbbox]: Invalid font filename...
    <yii 框架学习> yii 框架改为中文提示
    Yii 语言设置 中文提示信息
    yii新手在实例化models(controller调用models实化化)php warning错误
    yii CFormModel中的rules验证机制
    神舟优雅系列和神舟精盾系列哪个好?
    response.sendRedirect跳转 jsp:forward跳转
    jsp post/get中接处理
    jsp动作之 forward
    JDK eclipse selenium的安装以及环境变量的配置
  • 原文地址:https://www.cnblogs.com/Areas/p/2299573.html
Copyright © 2020-2023  润新知