Dav*_*yes 100 c# extended-properties
我试图找出如何读取/写入C#中的扩展文件属性,例如,您可以在Windows资源管理器中看到的注释,比特率,访问日期,类别等.任何想法如何做到这一点?编辑:我主要是读/写视频文件(AVI/DIVX/...)
csh*_*net 81
对于那些不为VB疯狂的人来说,这里是c#:
注意,您必须从"引用"对话框的"COM"选项卡添加对Microsoft Shell控件和自动化的引用.
public static void Main(string[] args)
{
List<string> arrHeaders = new List<string>();
Shell32.Shell shell = new Shell32.Shell();
Shell32.Folder objFolder;
objFolder = shell.NameSpace(@"C:\temp\testprop");
for( int i = 0; i < short.MaxValue; i++ )
{
string header = objFolder.GetDetailsOf(null, i);
if (String.IsNullOrEmpty(header))
break;
arrHeaders.Add(header);
}
foreach(Shell32.FolderItem2 item in objFolder.Items())
{
for (int i = 0; i < arrHeaders.Count; i++)
{
Console.WriteLine(
$"{i}\t{arrHeaders[i]}: {objFolder.GetDetailsOf(item, i)}");
}
}
}
Run Code Online (Sandbox Code Playgroud)
Mar*_*ade 27
有一个ID3阅读器的CodeProject文章.和kixtart.org上的一个帖子,其中包含其他属性的更多信息.基本上,您需要在文件夹 shell对象上调用该GetDetailsOf()方法.shell32.dll
Dir*_*mar 25
VB.NET中的这个示例读取所有扩展属性:
Sub Main()
Dim arrHeaders(35)
Dim shell As New Shell32.Shell
Dim objFolder As Shell32.Folder
objFolder = shell.NameSpace("C:\tmp")
For i = 0 To 34
arrHeaders(i) = objFolder.GetDetailsOf(objFolder.Items, i)
Next
For Each strFileName In objfolder.Items
For i = 0 To 34
Console.WriteLine(i & vbTab & arrHeaders(i) & ": " & objfolder.GetDetailsOf(strFileName, i))
Next
Next
End Sub
Run Code Online (Sandbox Code Playgroud)
您必须从" 引用"对话框的" COM"选项卡添加对Microsoft Shell控件和自动化的引用.
Mar*_*der 24
将以下NuGet包添加到项目中:
Microsoft.WindowsAPICodePack-Shell 由微软Microsoft.WindowsAPICodePack-Core 由微软using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.WindowsAPICodePack.Shell.PropertySystem;
string filePath = @"C:\temp\example.docx";
var file = ShellFile.FromFilePath(filePath);
// Read and Write:
string[] oldAuthors = file.Properties.System.Author.Value;
string oldTitle = file.Properties.System.Title.Value;
file.Properties.System.Author.Value = new string[] { "Author #1", "Author #2" };
file.Properties.System.Title.Value = "Example Title";
// Alternate way to Write:
ShellPropertyWriter propertyWriter = file.Properties.GetPropertyWriter();
propertyWriter.WriteProperty(SystemProperties.System.Author, new string[] { "Author" });
propertyWriter.Close();
Run Code Online (Sandbox Code Playgroud)
重要:
该文件必须是有效的文件,由特定的已分配软件创建.每种文件类型都有特定的扩展文件属性,并非所有文件属性都是可写的.
如果右键单击桌面上的文件而无法编辑属性,则无法在代码中对其进行编辑.
例:
Author或Title财产.所以只要确保使用一些 try catch
进一步的主题: MSDN:实现属性处理程序
谢谢你们这个帖子!当我想弄清楚exe的文件版本时,它帮助了我.但是,我需要弄清楚所谓的扩展属性的最后一点.
如果在Windows资源管理器中打开exe(或dll)文件的属性,则会获得"版本"选项卡以及该文件的"扩展属性"视图.我想访问其中一个值.
对此的解决方案是属性索引器FolderItem.ExtendedProperty,如果删除属性名称中的所有空格,您将获得该值.例如文件版本进入FileVersion,你有它.
希望这可以帮助其他人,只是想我会将此信息添加到此主题.干杯!
GetDetailsOf()方法 - 检索有关文件夹中项目的详细信息.例如,其大小,类型或上次修改的时间.文件属性可能因Windows-OS版本而异.
List<string> arrHeaders = new List<string>();
Shell shell = new ShellClass();
Folder rFolder = shell.NameSpace(_rootPath);
FolderItem rFiles = rFolder.ParseName(filename);
for (int i = 0; i < short.MaxValue; i++)
{
string value = rFolder.GetDetailsOf(rFiles, i).Trim();
arrHeaders.Add(value);
}
Run Code Online (Sandbox Code Playgroud)
用:
string propertyValue = GetExtendedFileProperty("c:\\temp\\FileNameYouWant.ext","PropertyYouWant");
Run Code Online (Sandbox Code Playgroud)
将在 Windows Server 2008 等 Windows 版本上工作,如果只是尝试正常创建 Shell32 对象,您将收到错误“无法将类型为 'System.__ComObject' 的 COM 对象转换为接口类型 'Shell32.Shell'”。
public static string GetExtendedFileProperty(string filePath, string propertyName)
{
string value = string.Empty;
string baseFolder = Path.GetDirectoryName(filePath);
string fileName = Path.GetFileName(filePath);
//Method to load and execute the Shell object for Windows server 8 environment otherwise you get "Unable to cast COM object of type 'System.__ComObject' to interface type 'Shell32.Shell'"
Type shellAppType = Type.GetTypeFromProgID("Shell.Application");
Object shell = Activator.CreateInstance(shellAppType);
Shell32.Folder shellFolder = (Shell32.Folder)shellAppType.InvokeMember("NameSpace", System.Reflection.BindingFlags.InvokeMethod, null, shell, new object[] { baseFolder });
//Parsename will find the specific file I'm looking for in the Shell32.Folder object
Shell32.FolderItem folderitem = shellFolder.ParseName(fileName);
if (folderitem != null)
{
for (int i = 0; i < short.MaxValue; i++)
{
//Get the property name for property index i
string property = shellFolder.GetDetailsOf(null, i);
//Will be empty when all possible properties has been looped through, break out of loop
if (String.IsNullOrEmpty(property)) break;
//Skip to next property if this is not the specified property
if (property != propertyName) continue;
//Read value of property
value = shellFolder.GetDetailsOf(folderitem, i);
}
}
//returns string.Empty if no value was found for the specified property
return value;
}
Run Code Online (Sandbox Code Playgroud)var folder = new Shell().NameSpace(folderPath);
foreach (FolderItem2 item in folder.Items())
{
var company = item.ExtendedProperty("Company");
var author = item.ExtendedProperty("Author");
// Etc.
}
Run Code Online (Sandbox Code Playgroud)
对于那些不能静态引用shell32的人,可以这样动态地调用它:
var shellAppType = Type.GetTypeFromProgID("Shell.Application");
dynamic shellApp = Activator.CreateInstance(shellAppType);
var folder = shellApp.NameSpace(folderPath);
foreach (var item in folder.Items())
{
var company = item.ExtendedProperty("Company");
var author = item.ExtendedProperty("Author");
// Etc.
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
101194 次 |
| 最近记录: |