简体   繁体   English

如何使用DirectoryInfo从C#中的服务器读取Word文件

[英]How to read word file from server in c# using DirectoryInfo

How to get a Word file from server in C#? 如何在C#中从服务器获取Word文件? I use following code: 我使用以下代码:

   static void Main(string[] args)
    {
        Word._Application application = new Word.Application();
        object fileformat = Word.WdSaveFormat.wdFormatXMLDocument;
     //  
        DirectoryInfo directory = new DirectoryInfo(@"http://www.sample.com/image/");
        foreach (FileInfo file in directory.GetFiles("*.doc", SearchOption.AllDirectories))
        {
            if (file.Extension.ToLower() == ".doc")
            {
                object filename = file.FullName;
                object newfilename = file.FullName.ToLower().Replace(".doc", ".docx");
                Word._Document document = application.Documents.Open(filename);

                document.Convert();
                document.SaveAs(newfilename, fileformat);
                document.Close();
                document = null;
            } 
        }
        application.Quit();
        application = null;
    }

but when I use this code to get file from local machine or desktop then work fine. 但是当我使用此代码从本地计算机或台式机获取文件时,工作正常。

Please tell me. 请告诉我。

You can't use DirectoryInfo with a URL. 您不能将DirectoryInfo与URL一起使用。

By design, this class only takes a local (or mapped network) path in its constructor. 根据设计,此类在其构造函数中仅采用本地(或映射的网络)路径。

You need to use System.Net.HttpWebRequest class to get the file from a URL, since it's located on a server on the internet, the only way to retrieve the file is to download it via HTTP . 您需要使用System.Net.HttpWebRequest类从URL获取文件,因为它位于Internet上的服务器上,因此检索文件的唯一方法是通过HTTP下载文件。

Edit: 编辑:

Based on your comments, you are looking to process 1 million files on a server you have access to. 根据您的评论,您希望在有权访问的服务器上处理100万个文件。 There are many ways to handle this. 有很多方法可以解决这个问题。

You can use a network path to the server, such as 您可以使用服务器的网络路径,例如

var di = new DirectoryInfo("\\servername\path\filename.doc")

You can just use a local path and create your application as a C# Console Application and use a local path. 您可以只使用本地路径,然后将您的应用程序创建为C#控制台应用程序,然后使用本地路径。 This is what I call a utility. 这就是我所说的实用程序。 It would be the faster method since it will process everything locally, and avoid network traffic. 这将是更快的方法,因为它将在本地处理所有内容,并避免网络流量。

var di = new DirectoryInfo("c:\your-folder\your-doc-file.doc")

Since you would run the C# console app directly on the server, the above would work. 由于您将直接在服务器上运行C#控制台应用程序,因此上述方法可行。

DirectoryInfo is just an object that contains information about a directory entry in your file system. DirectoryInfo只是一个对象,包含有关文件系统中目录条目的信息。 It doesn't download a file, which I presume is what you want to do. 它不会下载文件,我想这是您要执行的操作。

The code example at http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.connection(v=vs.110).aspx is, I think, similar to what you want. 我认为http://msdn.microsoft.com/zh-CN/library/system.net.httpwebrequest.connection(v=vs.110).aspx上的代码示例与您想要的类似。

DirectoryInfo is for accessing local files or UNC paths. DirectoryInfo用于访问本地文件或UNC路径。 You cannot use it to access a http addressed page. 您不能使用它来访问http地址页面。 You first need to download the file, ie using HttpWebRequest. 您首先需要下载文件,即使用HttpWebRequest。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM