Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SubType attribute of File element is csproj

Status
Not open for further replies.

KDavie

Programmer
Feb 10, 2004
441
US
In an application I am currently working on I have the need to programmatically add files to the csproj file. The task of doing this, in and of itself, is pretty straight forward. However, I'm having trouble finding any useful (and complete) documentation on the csproj file schema. Specifically, I am trying to determine all values that would be part of the SubType (attribute of File element) enumeration. Although I did find this xsd:


It does not contain an enumeration for the SubType attribute. After unsuccessfully googling this for hours last night I decided to just parse all my project files and find all unique subtypes. I parsed 73 files and came up with this list:

Code
code
Component
ASPXCodeBehind
Form
UserControl

Next I decided to parse the files again and determine SubTypes unique to file extenstions... Here is what I came up with:

Code - .cs, .asax.cs
code - .cs
Component - .cs, .asax
ASPXCodeBehind - .cs, .aspx.cs, .ascx.cs
Form - .aspx, .asmx
UserControl - .ascx

All the project files I parsed are VS 2003 projects and were generated by VS. Based on the results I found I can see no obvious rhyme or reason on how some of these SubTypes are determined. For example, .cs can be Code, code (different casing), Component, or ASPXCodeBehind. The component piece seems obvious enough, both asax and asax.cs are of Component SubType. Can anyone point me in the direction of useful documentation or explain to me exactly how these values are determined? It's important I maintain the integrity of our csproj files so although I feel I can make some assumptions I would rather know for sure.

Any help is appreciated.

Thanks,

-Kevin
 
I still haven't been able to find any useful documentation but I went ahead and put some code together based on what I can ascertain from the xsd and parsing the project files. Below is the code I put together... If anyone has any suggestions on how to improve it or what to add please let me know. Otherwise, hopefully this will help someone else in the future.

I've created a FileItem class which is meant to be instantiated and serialized. The root node of the serialized object can then be parsed out and added to the csproj.

FileItem class:

Code:
/// <summary>
/// CsProj File item
/// </summary>
[XmlRoot(ElementName="File")]
public class FileItem
{
    /// <summary>
    /// Default contstructor used for serialization
    /// </summary>
    public FileItem()
    {

    }
    /// <summary>
    /// Contstructor
    /// </summary>
    public FileItem(FileInfo fi, string relPath)
    {
        string ext = GetFileType(fi);
        this.RelPath = relPath;        
        this.BuildAction = GetBuildAction(ext).ToString();
        this.SubType = GetSubType(ext);
        this.DependentUpon = GetDependentUpon(ext, fi.Name);

    }

    #region Methods

    /// <summary>
    /// Returns file extension for given file
    /// </summary>
    /// <param name="fi">FileInfo object</param>
    /// <remarks>
    /// Since file names can contain arbitrary periods (.)
    /// use this method to retrieve accurate extension
    /// </remarks>

    public string GetFileType(FileInfo fi)
    {
        string name = fi.Name;
        string extension = "";
        int delimiters = name.Split('.').Length-1;
        if (delimiters == 1)
            return fi.Extension;

        for (int i = 0; i < ExtendedExtensionList.Count; i++)
        {
            extension = ExtendedExtensionList[i].ToString();
            if (name.IndexOf(extension) > -1)
                return extension;
        }
        
        return fi.Extension;

    }
    /// <summary>
    /// Returns value for BuildAction attribute
    /// </summary>
    /// <param name="fileName">name of file</param>
    public BuildActionValue GetBuildAction(string ext)
    {
        if (CompileTypeList.Contains(ext))
            return BuildActionValue.Compile;

        if (ContentTypeList.Contains(ext))
            return BuildActionValue.Content;

        if (EmbeddedResourceTypeList.Contains(ext))
            return BuildActionValue.EmbeddedResource;

        return BuildActionValue.None;
    }
    /// <summary>
    /// Returns value for SubType attribute
    /// </summary>
    /// <param name="fileName">name of file</param>
    /// <remarks>can return null value</remarks>
    public string GetSubType(string ext)
    {
        if(ASPXCodeBehindTypeList.Contains(ext))
            return SubTypeValue.ASPXCodeBehind.ToString();

        if(CodeTypeList.Contains(ext))
            return SubTypeValue.Code.ToString();

        if(ComponentTypeList.Contains(ext))
            return SubTypeValue.Component.ToString();

        if(FormTypeList.Contains(ext))
            return SubTypeValue.Form.ToString();

        if (UserControlTypeList.Contains(ext))
            return SubTypeValue.UserControl.ToString();

        return null;
    }
    /// <summary>
    /// Returns value for DependentUpon attribute
    /// </summary>
    /// <param name="ext">file extenion</param>
    /// <param name="fileName">name of file</param>
    /// <remarks>can return null value</remarks>
    public string GetDependentUpon(string ext, string fileName)
    {
        if (ExtendedExtensionList.Contains(ext))
            return fileName.Replace("." + fileName.Split('.')[fileName.Split('.').Length - 1], "");

        return null;
    }

    #endregion

    #region Xml Attributes

    private string relPath;
    /// <summary>
    /// Attribute of Type Element
    /// </summary>
    [XmlAttribute]
    public string RelPath
    {
        get { return relPath; }
        set { relPath = value; }
    }
    private string buildAction;
    /// <summary>
    /// Attribute of Type Element
    /// </summary>
    [XmlAttribute]
    public string BuildAction
    {
        get { return buildAction; }
        set { buildAction = value; }
    }
    private string subType;
    /// <summary>
    /// Attribute of Type Element
    /// </summary>
    [XmlAttribute]
    public string SubType
    {
        get { return subType; }
        set { subType = value; }
    }
    private string dependentUpon;
    /// <summary>
    /// Attribute of Type Element
    /// </summary>
    [XmlAttribute]
    public string DependentUpon
    {
        get { return dependentUpon; }
        set { dependentUpon = value; }
    }

    #endregion

    #region Properties

    private ArrayList extendedExtensionList;
    /// <summary>
    /// Collection of items contained in <see cref="KnownExtendedExtensions"/> enumeration
    /// </summary>
    public ArrayList ExtendedExtensionList
    {
        get
        {
            if (extendedExtensionList == null)
                FillTypeList(ref extendedExtensionList, typeof(KnownExtendedExtensions));

            return extendedExtensionList;

        }
    }

    private ArrayList contentTypeList;
    /// <summary>
    /// Collection of items contained in <see cref="KnownContentTypes"/> enumeration
    /// </summary>
    private ArrayList ContentTypeList
    {
        get
        {
            if (contentTypeList == null)
                FillTypeList(ref contentTypeList, typeof(KnownContentTypes));

            return contentTypeList;
        }
    }

    private ArrayList compileTypeList;
    /// <summary>
    /// Collection of items contained in <see cref="KnownCompileTypes"/> enumeration
    /// </summary>
    private ArrayList CompileTypeList
    {
        get
        {
            if (compileTypeList == null)
                FillTypeList(ref compileTypeList, typeof(KnownCompileTypes));

            return compileTypeList;
        }
    }

    private ArrayList embeddedResourceTypeList;
    /// <summary>
    /// Collection of items contained in <see cref="KnownEmbeddedResourceTypes"/> enumeration
    /// </summary>
    private ArrayList EmbeddedResourceTypeList
    {
        get
        {
            if (embeddedResourceTypeList == null)
                FillTypeList(ref embeddedResourceTypeList, typeof(KnownEmbeddedResourceTypes));

            return embeddedResourceTypeList;
        }
    }

    private ArrayList aspxCodeBehindTypeList;
    /// <summary>
    /// Collection of items contained in <see cref="KnownASPXCodeBehindTypes"/> enumeration
    /// </summary>
    private ArrayList ASPXCodeBehindTypeList
    {
        get
        {
            if (aspxCodeBehindTypeList == null)
                FillTypeList(ref aspxCodeBehindTypeList, typeof(KnownASPXCodeBehindTypes));

            return aspxCodeBehindTypeList;
        }
    }

    private ArrayList codeTypeList;
    /// <summary>
    /// Collection of items contained in <see cref="KnownCodeTypes"/> enumeration
    /// </summary>
    private ArrayList CodeTypeList
    {
        get
        {
            if (codeTypeList == null)
                FillTypeList(ref codeTypeList, typeof(KnownCodeTypes));

            return codeTypeList;
        }
    }

    private ArrayList componentTypeList;
    /// <summary>
    /// Collection of items contained in <see cref="KnownComponentTypes"/> enumeration
    /// </summary>
    private ArrayList ComponentTypeList
    {
        get
        {
            if (componentTypeList == null)
                FillTypeList(ref componentTypeList, typeof(KnownComponentTypes));

            return componentTypeList;
        }
    }

    private ArrayList formTypeList;
    /// <summary>
    /// Collection of items contained in <see cref="KnownFormTypes"/> enumeration
    /// </summary>
    private ArrayList FormTypeList
    {
        get
        {
            if (formTypeList == null)
                FillTypeList(ref formTypeList, typeof(KnownFormTypes));

            return formTypeList;
        }
    }

    private ArrayList userControlTypeList;
    /// <summary>
    /// Collection of items contained in <see cref="KnownUserControlTypes"/> enumeration
    /// </summary>
    private ArrayList UserControlTypeList
    {
        get
        {
            if (userControlTypeList == null)
                FillTypeList(ref userControlTypeList, typeof(KnownUserControlTypes));

            return userControlTypeList;
        }
    }

    

    #endregion

    #region Helper Functions

    /// <summary>
    /// Used to fill <see cref="ArrayList"/> properties with enumeration values
    /// </summary>
    /// <param name="list">ArrayList to fill</param>
    /// <param name="enumType">Enumeration type used to fill ArrayList</param>
    private void FillTypeList(ref ArrayList list, Type enumType)
    {
        list = new ArrayList();
        string[] types = Enum.GetNames(enumType);
        foreach (string t in types)
            list.Add(t.Replace("_", "."));
    }

    #endregion
}

Enumerators:

[code]
public enum SubTypeValue
{
    ASPXCodeBehind,
    Code,
    Component,
    Form,
    UserControl
}
public enum BuildActionValue
{
    Compile,
    Content,    
    EmbeddedResource,
    None
}

public enum KnownExtendedExtensions
{
    _aspx_cs,
    _asax_cs,
    _asmx_cs,
    _ascx_cs,
    _aspx_resx,
    _asax_resx,
    _asmx_resx,
    _ascx_resx
}
public enum KnownContentTypes
{
    _asax,
    _ascx,
    _asmx,
    _aspx,
    _bmp,
    _config,
    _css,
    _dll,
    _gif,
    _htc,
    _htm,
    _ico,
    _jpg,
    _js,
    _pdb,
    _png,
    _txt,
    _wav,
    _xml,
    _xsd
}
public enum KnownCompileTypes
{
    _asax_cs,
    _ascx_cs,
    _asmx_cs,
    _aspx_cs,
    _cs
}
public enum KnownEmbeddedResourceTypes
{
    _aspx_resx,
    _asax_resx,
    _asmx_resx,
    _ascx_resx
}

public enum KnownASPXCodeBehindTypes
{
    _ascx_cs,
    _aspx_cs
}
public enum KnownCodeTypes
{
    _asax_cs,
    _cs
}
public enum KnownComponentTypes
{
    _asax_cs
}
public enum KnownFormTypes
{
    _asmx,
    _aspx
}
public enum KnownUserControlTypes
{
    _ascx
}
[/code]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top