Following on from my previous post I thought I would briefly post about how easy it is to display a form or dialog to have more interaction with the user.
In this post, I will create a simple live writer plugin which will display an OpenFileDialog, then based on the file the user selects, the plugin will copy all the lines of text into the blog post. The OpenFileDialog could be replaced by a standard WinForm. If you didn’t read my previous post, then that would be a good place to start.
1) Like before, we need to create a C# class library and add references to the Live Writer API and System.Windows.Forms.
2) Add a WriterPluginAttribute and InsertableContentSourceAttribute to the class.
3) Inherit from ContentSource and override the CreateContent method.
4) Next, we implement the code. It’s very simple, first we create a OpenFileDialog. We then display the dialog saving the response in a DialogResult variable.
OpenFileDialog ofd = new OpenFileDialog();
DialogResult result = ofd.ShowDialog();
Depending on the result, if its not OK then we just return want to return the result to Live Writer. If its OK then it means that the user selected a file correctly to open. I then create a StreamReader to read all the lines of text into a string variable, then set the content out parameter so it can be inserted into the blog post.
if(result == DialogResult.OK)
StreamReader sr = new StreamReader(ofd.FileName);
string text = sr.ReadToEnd();
content = text;
5) Start Live Writer and you will see the plugin listed.
6) If you use the plugin, then all the line breaks will disappear because we are converting a file into HTML for the blog post. In order to fix this, we need to replace the line breaks with HTML line breaks.
text = text.Replace(“rn”, “
7) That solves the problem with line breaks, but we still lose white space. I’ll leave that for you to fix if you really want to – just need to place ‘ ‘ with ‘&nsbp’
If you select a file (text file) then the contents will be copied into the blog post.
I just wanted to demonstrate you have a lot of flexibly while creating the plugin’s so be creative!
Download code – InsertTextFromFilePlugin.cs.txt