使用C#打开Word文档(.doc)格式收到COMException原因由于文件阻止设置 [英] Opening Word-document (.doc) using C# causes COMException due to File Block Settings

查看:1156
本文介绍了使用C#打开Word文档(.doc)格式收到COMException原因由于文件阻止设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打开一个真的老了(二进制)的Word文件使用C#/。NET和的Microsoft.Office.Interop.Word。每当我尝试使用下面的代码片段打开此文件时,出现一个收到COMException告诉我的你正在试图打开被阻止在信任中心的文件阻止设置的文件类型。

这也可用于打开在MS Word同一个文件的时候,但调整的信任中心中的设置后,现在这只是正常的情况发生。但是,我还是不能使用C#打开该文件。有谁知道是否VS2010缓存这些设置,或在某处使用其自己的设置?我也试图创建一个新的项目,相同的片段,看看是否能帮助(固定设置在Word中后),但事实并非如此。

  Word.Application应用;

字符串文件=<文件名>中;

应用程序=新Word.Application();

尝试
{
    app.Documents.Open(文件);
}
赶上(收到COMException E)
{
    字符串s = e.Message;
}
 

解决方案

这是与备用数据流的,NTFS文件系统的支持。流语法:你的文件名访问这样一个流。存储用于拦截功能的文件中的信息流名为 Zone.Identifier:$ DATA 。它写的是什么程序复制的文件,通常是浏览器。

您可以看到他们的DIR / R选项。该 Sysinternals的流实用允许上市和删除它们。一个愚蠢的方式来摆脱它复制到不支持备用数据流,如.zip存档或闪存驱动器上的文件系统,并复制回来。

您可以看一下流的内容或类似记事本的文件名的命令编辑:zone.identifier ,你会看到这样的:

  [ZoneTransfer]
了zoneid = 3
 

在了zoneid值标识文件的来源。值是-1 =未指定,0 =本地计算机,1 =内部网,2 =信赖,3 =互联网,4 =不可信。什么记事本可以做的是却不可能在.NET中,它明确禁止使用字符的文件名。其目的是为人类解决这个问题,明确覆盖拦截功能文件。用鼠标右键单击在资源管理器,属性的文件,然后单击解除阻止按钮。该删除流。请注意,在第一个环节中提到的PowerShell命令。

I'm trying to open a really old (binary) Word-file using C#/.NET and Microsoft.Office.Interop.Word. Whenever I try to open this file using the snippet below, it fails with a COMException telling me that "You are attempting to open a file type that is blocked by your File Block settings in the Trust Center."

This also used to happen when opening the same file in MS Word, but after adjusting the settings found in the Trust Center this now works just fine. However, I still cannot open the file using C#. Does anyone know if VS2010 caches these settings, or use its own settings somewhere? I have also tried to create a new project (after fixing the settings in Word) with the same snippet to see if that helped, but it did not.

Word.Application app;

string file = "<filename>";

app = new Word.Application();

try
{
    app.Documents.Open(file);
}
catch (COMException e)
{
    string s = e.Message;
}

解决方案

This is implemented with an alternate data stream, supported by the NTFS file system. You access such a stream with the filename:stream syntax. The stream name that stores the info for the file blocking feature is Zone.Identifier:$DATA. It is written by whatever program copied the file, usually a browser.

You can see them with the DIR /R option. The SysInternals' Streams utility permits listing and deleting them. A silly way to get rid of it is copying it to a file system that doesn't support alternate data streams, like a .zip archive or a flash drive and copying it back.

You can look at the content of the stream or edit it with a command like notepad filename:zone.identifier, you'll see this:

[ZoneTransfer]
ZoneId=3

The ZoneId value identifies the origin of the file. Values are -1=unspecified, 0=local machine, 1=intranet, 2=trusted, 3=internet, 4=untrusted. What notepad can do is however not possible in .NET, it explicitly forbids using the : character in file names. The intention is for a human to deal with this, explicitly overriding the file blocking feature. Right-click the file in Explorer, Properties and click the Unblock button. That deletes the stream. Note the Powershell cmdlets mentioned in the first link.

这篇关于使用C#打开Word文档(.doc)格式收到COMException原因由于文件阻止设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆