示例#1
0
        /// <summary>
        /// Demonstrates how to read and write to/from a
        /// file at a certain file position
        /// </summary>
        protected void ReadWritePosDemo(Dog dog, Int32 fileId)
        {
            if ((null == dog) || !dog.IsLoggedIn())
            {
                return;
            }

            // firstly get a file object to a file.
            DogFile file = dog.GetFile(fileId);

            if (!file.IsLoggedIn())
            {
                // Not logged into dog - nothing left to do.
                return;
            }

            // we want to write an int at the end of the file.
            // therefore we are going to
            // - get the file's size
            // - set the object's read and write position to
            //   the appropriate offset.
            int       size   = 0;
            DogStatus status = file.FileSize(ref size);

            if (DogStatus.StatusOk != status)
            {
                return;
            }

            // set the file pos to the end minus the size of int
            file.FilePos = size - DogFile.TypeSize(typeof(int));

            // now read what's there
            int aValue = 0;

            status = file.Read(ref aValue);

            if (DogStatus.StatusOk != status)
            {
                return;
            }

            // write some data.
            status = file.Write(int.MaxValue);

            if (DogStatus.StatusOk != status)
            {
                return;
            }

            // read back the written value.
            int newValue = 0;

            status = file.Read(ref newValue);

            if (DogStatus.StatusOk == status)
            {
                // restore the original data.
                file.Write(aValue);
            }
        }
示例#2
0
        /// <summary>
        /// Demonstrates how to read and write to/from a
        /// file at a certain file position
        /// </summary>
        protected void ReadWritePosDemo(Dog dog, Int32 fileId)
        {
            if ((null == dog) || !dog.IsLoggedIn())
            {
                return;
            }

            Verbose("GetFileSize/FilePos Demo");

            // firstly get a file object to a file.
            DogFile file = dog.GetFile(fileId);

            if (!file.IsLoggedIn())
            {
                // Not logged into dog - nothing left to do.
                Verbose("Failed to get file object\r\n");
                return;
            }

            Verbose("Reading contents of file: " + file.FileId.ToString());
            Verbose("Retrieving the size of the file");

            // we want to write an int at the end of the file.
            // therefore we are going to
            // - get the file's size
            // - set the object's read and write position to
            //   the appropriate offset.
            int       size   = 0;
            DogStatus status = file.FileSize(ref size);

            ReportStatus(status);

            if (DogStatus.StatusOk != status)
            {
                Verbose("");
                return;
            }

            Verbose("Size of the file is: " + size.ToString() + " Bytes");
            Verbose("Setting file position to last int and reading value");

            // set the file pos to the end minus the size of int
            file.FilePos = size - DogFile.TypeSize(typeof(int));

            // now read what's there
            int aValue = 0;

            status = file.Read(ref aValue);
            ReportStatus(status);

            if (DogStatus.StatusOk != status)
            {
                Verbose("");
                return;
            }

            Verbose("Writing to file: 0x" + int.MaxValue.ToString("X2"));

            // write some data.
            status = file.Write(int.MaxValue);
            ReportStatus(status);

            if (DogStatus.StatusOk != status)
            {
                Verbose("");
                return;
            }

            // read back the written value.
            int newValue = 0;

            Verbose("Reading written data");
            status = file.Read(ref newValue);

            ReportStatus(status);
            if (DogStatus.StatusOk == status)
            {
                Verbose("Data read: 0x" + newValue.ToString("X2"));
            }

            // restore the original data.
            file.Write(aValue);
            Verbose("");
        }