简体   繁体   English

在日期以外的C#中修改文件属性

[英]Modify File Properties in C# Other Than Date

I'm working in .NET 3.5, Visual Studio 2010. I'm working on an Outlook Add-In that saves some email into a folder. 我正在.NET 3.5,Visual Studio 2010中工作。正在使用Outlook加载项,该加载项将一些电子邮件保存到文件夹中。 I've gotten it to work using the Microsoft.Office.Interop.Outlook.MailItem.SaveAs function. 我已经使用Microsoft.Office.Interop.Outlook.MailItem.SaveAs函数来使其工作。 However, the file properties have only the current time (time when the file was exported through the Add-In) as their Date Modified/Date Created etc., and other properties such as To, From, CC, BCC are not there. 但是,文件属性仅具有当前时间(通过外接程序导出文件的时间)作为其“修改日期/创建日期”等信息,而其他属性(例如“收件人”,“发件人”,“抄送”,“密件抄送”则不存在)。

If you open a folder in Windows Explorer (I'm using Windows 7), go to the top where it says Name, Date Modified, Type, etc., you can click on More, and see other various columns that might be relevant like "Album Artist", "To", "From", etc. 如果您在Windows资源管理器(我使用的是Windows 7)中打开文件夹,请转到顶部显示“名称”,“修改日期”,“类型”等的位置,然后单击“更多”,然后查看其他可能相关的其他列,例如“专辑艺术家”,“收件人”,“发件人”等

C# has a really easy way to modify the timings, File.SetCreationTime(filename, DateTime object); C#有一个非常简单的方法来修改时间, File.SetCreationTime(filename, DateTime object); . However, there's no .SetTo or .SetAlbumArtist or anything like that. 但是,没有.SetTo或.SetAlbumArtist或类似的东西。 How would I go about modifying those properties? 我将如何修改这些属性?

Update 1: through research, I found this link: Read/Write 'Extended' file properties (C#) , so that might contain the answer...but I have no idea how. 更新1:通过研究,我找到了以下链接: 读/写'扩展'文件属性(C#) ,因此可能包含答案...但是我不知道如何。 The accepted answer mentions running a method on a shell using a .dll. 接受的答案提到使用.dll在Shell上运行方法。 The second answer contains C# code, a commenter then asked basically what I want to know (how to modify the properties of a particular file), and the next commenter responded with "you can't set these"...so I'm still at square 1. 第二个答案包含C#代码,然后一个注释者基本上询问了我想知道的内容(如何修改特定文件的属性),下一个注释者回答“您不能设置这些” ...所以我仍然在广场1。

Update 2: I also tried the following: 更新2:我还尝试了以下操作:

foreach (Object selectedObject in explorer.Selection)
{
     Outlook.MailItem email = (selectedObject as Outlook.MailItem);
     //Modify the information about the email
     email.To = "I filled in To";
     email.SaveAs(filename, OlSaveAsType.olMSG);
}

This code successfully grabs the selected email(s) and save them under filename. 此代码成功获取选定的电子邮件并将其保存在文件名下。 However, the email.To = "I filled in To" changes the information when you open the .msg, but not the file properties. 但是,当您打开.msg时,email.To =“我填写到”会更改信息,但文件属性不会更改。

This cannot be changed, because it actually is not any file property in the terms of the filesystem (like file creation or modification datetime). 这不能更改,因为它实际上不是文件系统方面的任何文件属性(例如文件创建或修改日期时间)。

Columns in Windows Explorer you are talking about are "virtual" and they are "only" the feature of Windows Explorer. 您正在谈论的Windows资源管理器中的列是“虚拟的”,它们只是Windows资源管理器的功能。 It "understand" content of some file types and it can handle showing and sorting columns like that. 它“理解”某些文件类型的内容,并且可以处理像这样的显示和排序列。

If you want to change To, From etc. you have to change the content of the file you are saving, ie change the To or From in the message. 如果要更改“收件人”,“发件人”等,则必须更改要保存的文件的内容,即更改消息中的“收件人”或“发件人”。

To do so, if You have an Microsoft.Office.Interop.Outlook.MailItem object (which you are just saving), set desired properties on that object before you save it to file, ie: 为此,如果您有一个Microsoft.Office.Interop.Outlook.MailItem对象(您正在保存),则在将该对象保存到文件之前,对该对象设置所需的属性,即:

MailItem mail = ...;
mail.To = "some new to";
mail.Subject = "new subject";
mail.SaveAs(fileToSave, OlSaveAsType.OlMSG);

I don't know if it also changes email stored in the Outlook, if it so, create a copy of the email before changing properties 我不知道它是否还会更改Outlook中存储的电子邮件,如果更改了,请在更改属性之前创建电子邮件的副本

MailItem copyOfMailToSave = (MailItem)mail.Copy();

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

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