以非只读方式打开只读文件并保存/覆盖

发布于 2024-12-05 13:17:37 字数 399 浏览 0 评论 0原文

我有许多正在转换的 Word 文档。一切都很顺利,直到我得到一个只读文件。在这种情况下,我收到“另存为”提示。

有没有办法以读/写格式打开文件?我应该有管理员权限,所以访问不是问题。

我正在使用 VB.net 打开文件。更具体地说

doc = word.Documents.Open(path, Type.Missing, False, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)

I have a number of word documents I am converting. Everything is going great until I get a file that is read only. In this case I get a Save As prompt.

Is there any way to open the file in read/write format? I should have admin privileges so access isn't an issue.

I'm using VB.net to open the files. More specifically

doc = word.Documents.Open(path, Type.Missing, False, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

永不分离 2024-12-12 13:17:37

要打开只读文件,您需要将该属性设置为 false:

string path = "C:\\test.txt";
FileInfo info = new FileInfo(path);
info.IsReadOnly = false;
StreamWriter writer = new StreamWriter(path);
writer.WriteLine("This is an example.");
writer.Close();
info.IsReadOnly=true;

这是一个示例,但我确信它适用于 Word 文件。

编辑:

VB.NET 等效项:

Dim path As String = "C:\test.txt"
Dim info As FileInfo = New FileInfo(path)
info.IsReadOnly = False
Dim writer As StreamWriter = New StreamWriter(path)
writer.WriteLine("This is an example.")
writer.Close()
info.IsReadOnly = True

To open a read-only file you need to set that attribute to false:

string path = "C:\\test.txt";
FileInfo info = new FileInfo(path);
info.IsReadOnly = false;
StreamWriter writer = new StreamWriter(path);
writer.WriteLine("This is an example.");
writer.Close();
info.IsReadOnly=true;

This was an example but I'm sure it will work with word files.

EDIT:

VB.NET equivalent:

Dim path As String = "C:\test.txt"
Dim info As FileInfo = New FileInfo(path)
info.IsReadOnly = False
Dim writer As StreamWriter = New StreamWriter(path)
writer.WriteLine("This is an example.")
writer.Close()
info.IsReadOnly = True
GRAY°灰色天空 2024-12-12 13:17:37

在打开文件之前,请使用 FileInfo 类检查其属性。

如果 Attributes 属性包含 FileAttributes.ReadOnly,请更改它,文件将不再是只读的。

Before you open the file, check its Attributes with a FileInfo class.

If the Attributes property contains FileAttributes.ReadOnly, change it and the file will no longer be read-only.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文