System.Resources.ResourceReader.GetResourceData C# (CSharp) Méthode

GetResourceData() public méthode

public GetResourceData ( String resourceName, String &resourceType, byte &resourceData ) : void
resourceName String
resourceType String
resourceData byte
Résultat void
        public void GetResourceData(String resourceName, out String resourceType, out byte[] resourceData)
        {
            if (resourceName == null)
                throw new ArgumentNullException("resourceName");
            if (_resCache == null)
                throw new InvalidOperationException(Environment.GetResourceString("ResourceReaderIsClosed"));

            // Get the type information from the data section.  Also,
            // sort all of the data section's indexes to compute length of
            // the serialized data for this type (making sure to subtract
            // off the length of the type code).
            int[] dataPositions = new int[_numResources];
            int dataPos = FindPosForResource(resourceName);
            if( dataPos == -1) {
                throw new ArgumentException(Environment.GetResourceString("Arg_ResourceNameNotExist", resourceName));
            }
            
            lock(this) {
                // Read all the positions of data within the data section.
                for(int i=0; i<_numResources; i++) {
                    _store.BaseStream.Position = _nameSectionOffset + GetNamePosition(i);
                    // Skip over name of resource
                    int numBytesToSkip = _store.Read7BitEncodedInt();
                    _store.BaseStream.Position += numBytesToSkip;

                    dataPositions[i] = _store.ReadInt32();
                }
                Array.Sort(dataPositions);

                int index = Array.BinarySearch(dataPositions, dataPos);
                BCLDebug.Assert(index >= 0 && index < _numResources, "Couldn't find data position within sorted data positions array!");
                long nextData = (index < _numResources - 1) ? dataPositions[index + 1] + _dataSectionOffset : _store.BaseStream.Length;
                int len = (int) (nextData - (dataPos + _dataSectionOffset));
                BCLDebug.Assert(len >= 0 && len <= (int) _store.BaseStream.Length - dataPos + _dataSectionOffset, "Length was negative or outside the bounds of the file!");

                // Read type code then byte[]
                _store.BaseStream.Position = _dataSectionOffset + dataPos;
                ResourceTypeCode typeCode = (ResourceTypeCode) _store.Read7BitEncodedInt();
                resourceType = TypeNameFromTypeCode(typeCode);

                // The length must be adjusted to subtract off the number 
                // of bytes in the 7 bit encoded type code.
                len -= (int) (_store.BaseStream.Position - (_dataSectionOffset + dataPos));
                byte[] bytes = _store.ReadBytes(len);
                if (bytes.Length != len)
                    throw new FormatException(Environment.GetResourceString("BadImageFormat_ResourceNameCorrupted"));
                resourceData = bytes;
            }
        }

Usage Example

        private void build_page_1()
        {
            TextView tv1 = new TextView ();

            try
            {
                string rez = "Adeptus.Resources.resources";
                string key = "mystring1";
                string resourceType = "";
                byte[] resourceData;
                ResourceReader r = new ResourceReader(rez);
                r.GetResourceData (key, out resourceType, out resourceData);
                r.Close();
                System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
                tv1.Buffer.Text = enc.GetString (resourceData);
            }
            catch (Exception exp)
            {
                tv1.Buffer.Text = exp.Message;
            }

            tv1.WrapMode = WrapMode.Word;
            tv1.Editable = false;

            this.AppendPage (tv1);
            this.SetPageTitle (tv1, "Introduction");
            this.SetPageType (tv1, AssistantPageType.Intro);
            this.SetPageComplete (tv1, true);
        }
All Usage Examples Of System.Resources.ResourceReader::GetResourceData