Reputation: 13
I'm working on an extension for office using Interop assemblies, as part of this extension the primary goal of an exe I am building is to export PPTX slides as individual images.
I also want to create a text file that would contain some info on each slide. I am able to export the slides. I also create one text file but nothing is written to it. Each file should export as 1.txt, 2.txt...
class {
static void ConvertAndExportImagesAndTextFiles(ARGS){
//Some stuff that reads and prepares the slides for the export method
//Loops and exports several "objSlide" after taking args
foreach (Microsoft.Office.Interop.PowerPoint.Slide objSlide
in objActivePresentation.Slides)
{
i++;
string PathAndNumber = string.Concat(FilePath, i);
string PathAndNumberWithExt = string.Concat(PathAndNumber, ".txt");
FileStream fs = new FileStream(
PathAndNumberWithExt ,
FileMode.OpenOrCreate,
FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs);
try
{
sw.WriteLine("Howdy World.");
}
finally
{
?????????
}
objSlide.Export(FilePath, format, res, res);
}
I'm sorry, I am lost... The slides export because they are indexed by PowerPoint, But do I need to incmnt something here?
Upvotes: 1
Views: 4409
Reputation: 61402
Just put your FileStream and StreamWriter into a using
clause. The problem is that your writes are buffered, and until you call Close
(or have using
do that for you), that buffer remains in memory, and doesn't get written out.
using (var sw = new StreamWriter(new FileStream(...)))
{
sw.WriteLine("Howdy World.");
}
As pointed out by the commenters, though, you might want to use a shorthand:
using (var sw = File.CreateText(...))
{
sw.WriteLine("Howdy World.");
}
Upvotes: 2