Exemple #1
0
        /// <summary>
        /// Attempts to call the open fileName method for any IDataProvider plugin that matches the extension on the string.
        /// </summary>
        /// <param name="fileName">A String fileName to attempt to open.</param>
        /// <param name="inRam">A boolean value that if true will attempt to force a load of the data into memory. This value overrides the property on this DataManager.</param>
        /// <param name="progressHandler">Specifies the progressHandler to receive progress messages. This value overrides the property on this DataManager.</param>
        /// <param name="providerName">Name of the provider that should be used for opening. If it is not set or the provider can't open the file, DS takes the first provider that can open the file.</param>
        public virtual IDataSet OpenFile(string fileName, bool inRam, IProgressHandler progressHandler, string providerName = "")
        {
            string ext = Path.GetExtension(fileName);

            if (ext != null)
            {
                // Fix by Ted Dunsford, moved "ToLower" operation from the previous location on the filename
                // which would mess up the actual filename identification, and only use the lower case for the extent testing.
                ext = ext.ToLower();
                IDataSet result;

                if (providerName != "") // if a provider name was given we try to find this provider and use it to open the file
                {
                    var provider = PreferredProviders.FirstOrDefault(kvp => kvp.Value.Name == providerName);
                    if (provider.Value != null)
                    {
                        if (GetSupportedExtensions(provider.Value.DialogReadFilter).Contains(ext))
                        {
                            result = provider.Value.Open(fileName);
                            if (result != null)
                            {
                                return(result);
                            }
                        }
                    }
                    var dp = DataProviders.FirstOrDefault(kvp => kvp.Name == providerName);
                    if (dp != null)
                    {
                        if (GetSupportedExtensions(dp.DialogReadFilter).Contains(ext))
                        {
                            result = dp.Open(fileName);
                            if (result != null)
                            {
                                return(result);
                            }
                        }
                    }
                }

                // Check for the extension in the preferred plugins list
                if (PreferredProviders.ContainsKey(ext))
                {
                    result = PreferredProviders[ext].Open(fileName);
                    if (result != null)
                    {
                        return(result);
                    }
                    // if we get here, we found the provider, but it did not succeed in opening the file.
                }

                // Check the general list of developer specified providers... but not the directory providers
                foreach (IDataProvider dp in DataProviders)
                {
                    if (!GetSupportedExtensions(dp.DialogReadFilter).Contains(ext))
                    {
                        continue;
                    }
                    // attempt to open with the fileName.
                    dp.ProgressHandler = ProgressHandler;

                    result = dp.Open(fileName);
                    if (result != null)
                    {
                        return(result);
                    }
                }
            }

            throw new ApplicationException(DataStrings.FileTypeNotSupported);
        }