所以這是我的問題 - 我有很多檔案都具有相同的檔案擴展名(例如,.aabc),但檔案型別卻大不相同。有些可能是電子郵件,有些可能是視頻。重點是,我不知道這些檔案是什么,我需要將它們轉換為可用的檔案,但要做到這一點,我需要找出它們是什么。
我已經嘗試過Mime-Detective nuget 擴展,但它不想作業。“ContentInspectorBuilder”類沒有像 GitHub 上的示例代碼那樣的檢查方法。下面是我為它所做的
using System;
using System.IO;
using System.Security;
using System.Windows.Forms;
using MimeDetective;
namespace MimeDetectiveTest
{
public partial class Form1 : Form
{
private OpenFileDialog openFile;
private ContentInspectorBuilder inspector;
public Form1()
{
InitializeComponent();
openFile = new OpenFileDialog();
inspector = new ContentInspectorBuilder()
{
Definitions = new MimeDetective.Definitions.ExhaustiveBuilder()
{
UsageType = MimeDetective.Definitions.Licensing.UsageType.PersonalNonCommercial
}.Build()
};
}
private String findFileType(String path)
{
var content = ContentReader.Default.ReadFromFile(path);
var results = ContentInspectorExtensions.Inspect(inspector, content);
return results.ByFileExtension().ToString();
}
private void openFileButton_Click(object sender, EventArgs e)
{
if (openFile.ShowDialog() == DialogResult.OK)
{
try
{
var filePath = openFile.FileName;
var fileExtension = Path.GetExtension(filePath);
var newFileExtension = findFileType(filePath);
filePathLabel.Text = filePath;
extensionLabel.Visible = true;
extensionLabel.Text = "File Identified - " newFileExtension;
}
catch (SecurityException ex)
{
MessageBox.Show($"Security error.\n\nError message: {ex.Message}\n\n"
$"Details:\n\n{ex.StackTrace}");
}
}
}
}
}
我最接近它的作業方式是將這個Github 用于另一個 Mime-Detective但是如果我發送特定的檔案格式(例如 .xml 或 .flac),整個事情就會崩潰或不輸出任何內容(.htm as一個例子)。代碼與上面的類似,只需從代碼的起始位移除檢查器并將 findFileType 方法更改為:
private String findFileType(String path)
{
Stream fileDataStream = File.Open(path, FileMode.Open);
FileType fileType = fileDataStream.GetFileType();
return fileType.Extension;
}
uj5u.com熱心網友回復:
我想你只是錯過了一個Build()。
如果比較github上的示例代碼:
var Inspector = new ContentInspectorBuilder() {
Definitions = new Definitions.ExhaustiveBuilder() {
UsageType = Definitions.Licensing.UsageType.PersonalNonCommercial
}.Build()
}.Build(); // <=====
到您的代碼:
inspector = new ContentInspectorBuilder()
{
Definitions = new MimeDetective.Definitions.ExhaustiveBuilder()
{
UsageType = MimeDetective.Definitions.Licensing.UsageType.PersonalNonCommercial
}.Build()
}; // <=====
您會發現它們對構建器構建的結果(即 a ContentInspector,而不是 a ContentInspectorBuilder)進行操作,而不是對構建器本身進行操作。這也解釋了缺少的方法。
實際上,我想知道這條線如何
var results = ContentInspectorExtensions.Inspect(inspector, content);
沒有扔。還是做到了?我在 Builder 型別上找不到匹配的擴展名。所以,我會預料到一個相關的錯誤。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/348000.html
