Beispiel #1
0
        /// <summary>
        /// Demonstrates how to perform read and write
        /// operations on a file in SuperDog
        /// </summary>
        protected void ReadWriteDemo(Dog dog, Int32 fileId)
        {
            if ((null == dog) || !dog.IsLoggedIn())
            {
                return;
            }
            // Get a file object to a file in SuperDog.
            // please note: the file object is tightly connected
            // to its dog object. logging out from a dog also
            // invalidates the file object.
            // doing the following will result in an invalid
            // file object:
            // dog.login(...)
            // DogFile file = dog.GetFile();
            // dog.logout();
            // Debug.Assert(file.IsValid()); will assert
            DogFile file = dog.GetFile(fileId);

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

            // get the file size
            int       size   = 0;
            DogStatus status = file.FileSize(ref size);

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

            // read the contents of the file into a buffer
            byte[] bytes = new byte[size];
            status = file.Read(bytes, 0, bytes.Length);

            if (DogStatus.StatusOk != status)
            {
                return;
            }
            // now let's write some data into the file
            byte[] newBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7 };

            status = file.Write(newBytes, 0, newBytes.Length);
            if (DogStatus.StatusOk != status)
            {
                return;
            }
            // and read them again
            status = file.Read(newBytes, 0, newBytes.Length);
            // restore the original contents
            file.Write(bytes, 0, bytes.Length);
        }
Beispiel #2
0
        public static string ReadData()
        {
            DogFeature feature = DogFeature.Default;

            using (Dog dog = new Dog(feature))
            {
                DogStatus status = dog.Login(VendorCode.Code, scope);
                if ((null == dog) || !dog.IsLoggedIn())
                {
                    return("请插入加密狗");
                }
                DogFile file = dog.GetFile(FileId);
                if (!file.IsLoggedIn())
                {
                    // Not logged into a dog - nothing left to do.
                    return("加载数据失败");
                }
                int size = 0;
                status = file.FileSize(ref size);
                if (DogStatus.StatusOk != status)
                {
                    return("加载数据失败");
                }

                // read the contents of the file into a buffer
                byte[] bytes = new byte[size];
                status = file.Read(bytes, 0, bytes.Length);
                if (DogStatus.StatusOk != status)
                {
                    return("失败");
                }
                return(System.Text.Encoding.UTF8.GetString(bytes));
            }
        }
Beispiel #3
0
        private void button2_Click(object sender, EventArgs e)
        {
            DogFeature feature = DogFeature.Default;

            using (Dog dog = new Dog(feature))
            {
                DogStatus status = dog.Login(VendorCode.Code, scope);
                if ((null == dog) || !dog.IsLoggedIn())
                {
                    return;
                }
                DogFile file = dog.GetFile(FileId);
                if (!file.IsLoggedIn())
                {
                    // Not logged into a dog - nothing left to do.
                    return;
                }
                int size = 0;
                status = file.FileSize(ref size);
                if (DogStatus.StatusOk != status)
                {
                    return;
                }

                // read the contents of the file into a buffer
                byte[] bytes = new byte[size];
                status = file.Read(bytes, 0, bytes.Length);
                if (DogStatus.StatusOk != status)
                {
                    return;
                }
                this.textBox4.Text = System.Text.Encoding.UTF8.GetString(bytes);
            }
        }
Beispiel #4
0
        private void button1_Click(object sender, EventArgs e)
        {
            DogFeature feature = DogFeature.Default;

            using (Dog dog = new Dog(feature))
            {
                DogStatus status = dog.Login(VendorCode.Code, scope);
                if ((null == dog) || !dog.IsLoggedIn())
                {
                    return;
                }
                DogFile file = dog.GetFile(FileId);
                if (!file.IsLoggedIn())
                {
                    // Not logged into a dog - nothing left to do.
                    return;
                }
                int size = 0;
                status = file.FileSize(ref size);
                if (DogStatus.StatusOk != status)
                {
                    return;
                }

                byte[] newBytes = System.Text.Encoding.UTF8.GetBytes(this.textBox1.Text);//new byte[] { 1, 2, 3, 4, 5, 6, 7 };

                status = file.Write(newBytes, 0, newBytes.Length);
                if (DogStatus.StatusOk != status)
                {
                    return;
                }



                // read the contents of the file into a buffer
                byte[] bytes = new byte[size];
                status = file.Read(bytes, 0, bytes.Length);
                if (DogStatus.StatusOk != status)
                {
                    return;
                }
                this.textBox2.Text = System.Text.Encoding.UTF8.GetString(bytes);
            }
            //textBox3.Text = dapi.WriteData(this.textBox1.Text.Trim());
            //this.textBox2.Text = dapi.ReadDate();
            //textBox3.SelectionStart = textBox3.TextLength;
            //textBox3.ScrollToCaret();
            //textBox3.Refresh();
        }
Beispiel #5
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);
            }
        }
Beispiel #6
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("");
        }
Beispiel #7
0
        /// <summary>
        /// Demonstrates how to perform read and write
        /// operations on a file in SuperDog
        /// </summary>
        protected void ReadWriteDemo(Dog dog, Int32 fileId)
        {
            if ((null == dog) || !dog.IsLoggedIn())
            {
                return;
            }

            Verbose("Read/Write Demo");

            // Get a file object to a file in SuperDog.
            // please note: the file object is tightly connected
            // to its dog object. logging out from a dog also
            // invalidates the file object.
            // doing the following will result in an invalid
            // file object:
            // dog.login(...)
            // DogFile file = dog.GetFile();
            // dog.logout();
            // Debug.Assert(file.IsValid()); will assert
            DogFile file = dog.GetFile(fileId);

            if (!file.IsLoggedIn())
            {
                // Not logged into a 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");

            // get the file size
            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");

            // read the contents of the file into a buffer
            byte[] bytes = new byte[size];

            Verbose("Reading data");
            status = file.Read(bytes, 0, bytes.Length);
            ReportStatus(status);

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

            DumpBytes(bytes);

            Verbose("Writing to file");

            // now let's write some data into the file
            byte[] newBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7 };

            status = file.Write(newBytes, 0, newBytes.Length);
            ReportStatus(status);
            if (DogStatus.StatusOk != status)
            {
                Verbose("");
                return;
            }

            DumpBytes(newBytes);

            // and read them again
            Verbose("Reading written data");
            status = file.Read(newBytes, 0, newBytes.Length);
            ReportStatus(status);
            if (DogStatus.StatusOk == status)
            {
                DumpBytes(newBytes);
            }

            // restore the original contents
            file.Write(bytes, 0, bytes.Length);
            Verbose("");
        }