5

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

1 回答 1

7

这是通过 NTFS 文件系统支持的备用数据流实现的。filename:stream您可以使用语法访问这样的流。存储文件阻止功能信息的流名称是Zone.Identifier:$DATA. 它是由复制文件的任何程序编写的,通常是浏览器。

您可以使用 DIR /R 选项查看它们。SysInternals 的 Streams 实用程序允许列出和删除它们。摆脱它的一种愚蠢方法是将其复制到不支持备用数据流的文件系统中,例如 .zip 存档或闪存驱动器,然后将其复制回来。

您可以查看流的内容或使用类似的命令对其进行编辑notepad filename:zone.identifier,您将看到:

[ZoneTransfer]
ZoneId=3

ZoneId 值标识文件的来源。值为 -1=未指定,0=本地计算机,1=内联网,2=受信任,3=互联网,4=不受信任。然而,记事本可以做的事情在 .NET 中是不可能的,它明确禁止:在文件名中使用字符。目的是让人类来处理这个问题,明确地覆盖文件阻止功能。右键单击资源管理器中的文件,属性,然后单击取消阻止按钮。这会删除流。请注意第一个链接中提到的 Powershell cmdlet。

于 2013-11-05T14:17:10.740 回答