Click here to Skip to main content
12,703,063 members (29,763 online)
Click here to Skip to main content
Articles » Languages » C# » Utilities » Downloads

Stats

80.5K views
1.4K downloads
60 bookmarked
Posted

Creating a Custom Tool to Generate Multiple Files in Visual Studio 2005

, 30 Nov 2006
Visual Studio provides interfaces for extending the environment to generate one file from another through a &quot;Custom Tool&quot;. <br>Now, learn how to generate multiple files, easily, and extensibly.
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Text.RegularExpressions;
using System.Net;

namespace VsMultipleFileGenerator
{
    [Guid("6EE05D8F-AAF9-495e-A8FB-143CD2DC03F5")]
    public class HtmlImageEmbedderCustomTool : VsMultipleFileGenerator<string>
    {


        public override IEnumerator<string> GetEnumerator()
        {
            Stream inStream = File.OpenRead(base.InputFilePath);
            Regex regAnchor = new Regex("<img src=[\"']([^\"']+)[\"'][^>]+[/]?>", RegexOptions.IgnoreCase);
            try
            {
                StreamReader reader = new StreamReader(inStream);
                string line = null;
                while ((line = reader.ReadLine()) != null)
                {
                    MatchCollection mc = regAnchor.Matches(line);
                    foreach (Match match in mc)
                    {
                        // yield each element to the enumerator
                        yield return match.Groups[1].Value;
                    }
                }
            }
            finally
            {
                inStream.Close();
            }
        }

        protected override string GetFileName(string element)
        {
            return element.Substring(element.LastIndexOf('/') + 1);
        }

        public override byte[] GenerateContent(string element)
        {
            // create the image file
            WebRequest getImage = WebRequest.Create(element);

            return StreamToBytes(getImage.GetResponse().GetResponseStream());
        }

        public override byte[] GenerateSummaryContent()
        {
            // Im not going to put anything in here...
            return new byte[0];
        }

        public override string GetDefaultExtension()
        {
            return ".txt";
        }

        protected byte[] StreamToBytes(Stream stream)
        {
            MemoryStream outBuffer = new MemoryStream();

            byte[] buffer = new byte[1024];
            int count = 0;
            while ((count = stream.Read(buffer, 0, buffer.Length)) > 0)
            {
                outBuffer.Write(buffer, 0, count);
            }

            return outBuffer.ToArray();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Adam Langley
Software Developer (Senior)
New Zealand New Zealand
"Find a job you love, and you'll never work a day in your life."

Adam Langley is a software engineer in Auckland, New Zealand.

Software development is his personal passion, he takes pride in his work, and likes to share his experiences with the development community.

When he's not coercing computers, you'll find him riding his motorcycle, or attempting to carve something creative from bone.

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 1 Dec 2006
Article Copyright 2006 by Adam Langley
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid