[英]How to open PowerPoint file in resource file C#
我的資源文件夾中有許多小的PowerPoint
文件,我想打開它們。 我在執行此操作時遇到問題,因為Resource.sendToPPTTemp
的類型為byte[]
,要打開文件,我需要將它作為字符串。 有沒有一種方法可以從資源中以字符串形式打開文件?
var file = Resources.sendToPPTTemp;
ppnt.Application ppntApplication = new ppnt.Application();
var _assembly = Assembly.GetExecutingAssembly();
var myppnt = ppntApplication.Presentations.Open(file.ToString());
ppntApplication.Visible = MsoTriState.msoTrue;
您需要將文件路徑提供給Open
方法,而不是二進制表示形式。 您要么擁有路徑並將其傳遞給方法,要么必須使用byte []創建文件。
我寧願使用所有PPT創建一個文件夾,並將該文件夾的路徑存儲在資源文件中。 然后,您可以使用第一種方法:
var di = new DirectoryInfo(Resources.PPTFolderPath);
foreach(var file in di.GetFiles())
{
var myppnt = ppntApplication.Presentations.Open(fi.FullName);
ppntApplication.Visible = MsoTriState.msoTrue;
[..]
}
但是,如果您確實要將PPT存儲在資源文件中,則可以使用一個臨時文件來做到這一點,例如:
var tmpPath = Path.GetTempFileName();
try
{
File.WriteAllBytes(tmpPath, Resources.sendToPPTTemp);
var myppnt = ppntApplication.Presentations.Open(tmpPath);
ppntApplication.Visible = MsoTriState.msoTrue;
[..]
}
finally
{
// you have to delete your tmp file at the end!!!
// probably not the better way to do it because I guess the program does not block on Open.
// Better store the file path into a list and delete later.
var fi = new FileInfo(tmpPath);
fi.Delete();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.