我正在开发一个应用程序,将一些.msg文件的内容记录到SQL数据库中。
.msg文件也来自SMS服务器,只是为了解决我遇到的问题。
每次我在服务器上运行脚本时,我都会得到“提供的文件不是有效的IStorage”,谷歌似乎对这个问题毫无结果,这让我很生气!
我也在使用这个库:https://github.com/Sicos1977/MSGReader
using MsgReader.Outlook;
using System;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
namespace OutboxSMS
{
class Program
{
static void Main(string[] args)
{
PostmsgtoSQL();
Console.ReadLine();
}
static bool Filecreated = true;
public static void OnChanged(object source, FileSystemEventArgs e)
{
Filecreated = true;
}
public static void PostmsgtoSQL()
{
var directory = new DirectoryInfo("C:\\ProgramData\\Felltech Ltd\\OutboxSMSXX\\SMSTextSent");
var myFile = directory.GetFiles().OrderByDescending(f => f.LastWriteTime).First();
var filepath = myFile.ToString();
//File System Watching
FileSystemWatcher FSW = new FileSystemWatcher(directory.ToString(), ".log");
FSW.Created += new FileSystemEventHandler(OnChanged);
FSW.EnableRaisingEvents = true;
//below is broken :<
using (var msg = new Storage.Message(@filepath))
{
//Grab .msg headers
var Id = msg.Id;
var datetime = msg.CreationTime;
var from = msg.Sender;
var sentOn = msg.SentOn;
var recipientsTo = msg.GetEmailRecipients(Storage.Recipient.RecipientType.To, false, false);
var subject = msg.Subject;
var messagebody = msg.BodyRtf;
//broken to here
if (Filecreated == true)
{
//SQL Wizardry
SqlConnection db = new SqlConnection();
db.ConnectionString = "Server=Dev-SQL;Database=AshTest;Trusted_Connection=true";
String query = "INSERT INTO dbo.SMSLog (ID,toaddress,fromaddress,subject,datetime,messagebody) VALUES(@Id,@toaddress,@fromaddress, @subject, @datetime.)";
db.Open();
SqlCommand command = new SqlCommand(query, db);
command.Parameters.AddWithValue("@ID", Id);
command.Parameters.AddWithValue("@toaddress", recipientsTo);
command.Parameters.AddWithValue("@fromaddress", from);
command.Parameters.AddWithValue("@subject", subject);
command.Parameters.AddWithValue("@datetime", datetime);
command.Parameters.AddWithValue("@messagebody", messagebody);
command.ExecuteNonQuery();
db.Close();
db.Dispose();
}
}
}
}
}
答案 0 :(得分:0)
此库调用StgIsStorageFile
方法,但丢弃结果。在库中更改这两行,如下所示:
if (NativeMethods.StgIsStorageFile(storageFilePath) != 0)
{
throw new ArgumentException("The provided file is not a valid IStorage", "storageFilePath");
}
对于类似的东西:
int result = NativeMethods.StgIsStorageFile(storageFilePath);
if (result != 0)
{
throw new ArgumentException($"The provided file is not a valid IStorage. Result={result}", "storageFilePath");
}
返回值在here中进行了描述,应该可以帮助您找到正在发生的事情。