Sharepoint - 使用 C# 打开存储在 Sharepoint 文档库中的 word doc 文件

问题描述:

我正在使用 Visual Studio C# 进行编码.像上面提到的那样,有什么方法可以打开存储在 Sharepoint 文档库中的 word 文件?只需要打开word文件让我阅读.不必编辑或删除.

I'm coding using visual studio, C#. Like the above mentioned, is there any ways i can open a word file stored in Sharepoint's document library? Just have to open the word file for me to read. Do not have to edit or delete.

我试过:System.Diagnostics.Process.Start("iexplore.exe",url) 但它似乎不起作用.

I have tried: System.Diagnostics.Process.Start("iexplore.exe",url) but it doesn't seems to work.

尝试了以下代码.显示我的标签没问题.但是 doc 这个词也不会启动.

Tried the following codes. No problem displaying my label. But the word doc wouldn't launch as well.

protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = GridView1.Rows[index];
            this.Label1.Text = "You selected " + row.Cells[4].Text + ".";
            System.Diagnostics.Process.Start(@"C:\Users\Administrator\Desktop\Test\Test.docx");
        }  
    }

这可能比听起来要困难得多.如果您尝试从服务器端对象模型执行此操作,那么您很可能会在会话 0 中运行.会话 0 是一个特殊的 Windows 服务会话,它不允许产生新进程.要解决此问题,您必须通过使用 Windows API 登录用户然后控制他们的会话来退出会话 0.我不会在这条路上走得更远,因为你没有说明你实际上正在这样做.

This could potentially be a lot harder than it sounds. If you're trying to do this from the server-side object model, then you'll most likely be running in session 0. Session 0 is a special Windows session for services, which doesn't permit the spawning of new processes. To get around this, you'd have to get out of session 0 by using the Windows API to log a user in and then take control of their session. I'll go no further down this path, since you've not specified that you're actually doing this.

如果您只是想下载存储在 SharePoint 文档库中的 Word 文档,然后在客户端启动它,您可以使用以下方法下载并打开文档:

If you're just looking to download a word document stored in a document library on SharePoint and then start it on the client side, you can use the following to download and open the document:

ClientContext clientContext = new ClientContext("http://SharePoint/");
string serverRelativeUrlOfFile = "SharedDocuments/file.docx";
string fileDestinationPath = @"C:\Documents\file.docx";

using (FileInformation sharePointFile =
    Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, serverRelativeUrlOfFile))
{
    using (Stream destFile = System.IO.File.OpenWrite(fileDestinationPath))
    {
        byte[] buffer = new byte[8 * 1024];
        int byteReadInLastRead;
        while ((byteReadInLastRead = sharePointFile.Stream.Read(buffer, 0, buffer.Length)) > 0)
        {
            destFile.Write(buffer, 0, byteReadInLastRead);
        }
    }
}


System.Diagnostics.Process.Start(@"C:\Documents\file.docx");

请注意,这里使用 SharePoint2010 客户端对象模型,因为会话 0 隔离使服务器端对象模型的步骤变得相当困难,除非有要求,否则我不会在此处详细说明.

Note, this uses the SharePoint2010 Client object model, as the steps for the server-side object model are made considerably harder by session 0 isolation, and I won't detail that here unless it's requested.

在评论之后,很明显您正在尝试使用 Webpart 在客户端下载和执行文件.以上代码来自 SharePoint 2010 客户端对象模型,但为 C# 版本.既然我已经向您展示了正确的 C# 版本,那么将它转换为 JavaScript 版本应该很简单.这会将文件下载到客户端.下载文件后,使用以下 JavaScript/ActiveX 执行文件:

After the comments, it's clear that you're trying to use a Webpart to download and execute a file on the client side. The above code is from the SharePoint 2010 Client Object Model, but the C# version. Translating it to the JavaScript version should be trivial now that I've shown you the correct C# version. This will download the file to the client. Once the file is downloaded, execute the file using the following JavaScript/ActiveX:

 var objShell = new ActiveXObject("Shell.Application");
 var strArguments = "";
 var strDirectory = "";
 var strOperation = "open";
 var intShow = 1;
 objShell.ShellExecute(FileLocation, strArguments, strDirectory, strOperation, intShow);

替换正确参数的地方.请注意,这只能在 Internet Explorer 上运行——因为这个明显非常不安全的 ActiveX 片段被市面上所有的浏览器都拦截了——而且我只在 IE8 上对其进行了测试.

Where the correct parameters are substituted. Note that this will only work on Internet Explorer -- as this obviously hilariously unsafe piece of ActiveX is blocked by every half-decent browser out there -- and I've only tested it on IE8.