Other file extensions for OpenDocumentCommand

2 posts, 1 answers
  1. Kenneth
    Kenneth avatar
    101 posts
    Member since:
    Apr 2014

    Posted 29 Aug 2014 Link to this post

    I have RTF files that have an extension other than RTF

    I would like to have the OpenDocumentCommand of the RichTexctBox do one of two things:

    1. When the "All Files" filter is selected have it not rely on the file extension to determine the file type.

    My reasoning is that if the file were of one of the 'supported' file extensions then the user would not need to use the 'All Files' option.
    Currently if I select the "All Files" filter, then select an RTF file with an extension other than .rtf (say .cci) the editor complains that the file is of an unsupported type.
    If, however, I rename the file to .rtf it will open.

    2. Alternately allow me to add my own filters (like *.cci) and associate a format provider of the appropriate type (in this case RTF).

    Now I could just take over the whole process and use my own OpenFileDialog in place of the OpenDocumentCommand, but if there is a way to extend the OpenDocumentCommand that would be nice.
  2. Answer
    Petya
    Admin
    Petya avatar
    975 posts

    Posted 02 Sep 2014 Link to this post

    Hello Kenneth,

    The filters in the OpenFileDialog and SaveFileDialog shown when the open and save commands are invoked respectively correspond to the format providers that are currently registered. Basically, once you add a reference to one of the format provider assemblies, the respective format provider is going to be registered in the DocumentFormatProvidersManager.FormatProviders collection along with its supported extension(s).

    Other than taking over for the commands and showing the dialogs yourself, you could create custom providers based on the default ones and specify your desired supported extensions. For example, the next snippet creates a provider that would import/export RTF document with .cci file extension:
    public class FakeFormatProvider : RtfFormatProvider
    {
        public override IEnumerable<string> SupportedExtensions
        {
            get
            {
                return new string[] { ".cci" };
            }
        }
     
        public override string Name
        {
            get
            {
                return "FakeFormatProvider";
            }
        }
     
        public override string FilesDescription
        {
            get
            {
                return "Other Rich Text Documents";
            }
        }
    }

    To use the provider with the open and save commands you need to register it like this:
    DocumentFormatProvidersManager.RegisterFormatProvider(new FakeFormatProvider());

    I hope this is helpful.

    Regards,
    Petya
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  3. DevCraft banner
Back to Top