public bool WriteMessageFirst(Hasp hasp, HaspFileId fileId, string str) { try { HaspFile file = hasp.GetFile(fileId); file.Write(str); return(true); } catch { return(false); } }
public string ReadToStr(Hasp hasp, HaspFileId fileId) { string dd = ""; try { HaspFile file = hasp.GetFile(fileId); file.Read(ref dd); return(dd); } catch { return(null); } }
/// <summary> /// Demonstrates how to read and write to/from a key's /// file at a certain file position /// </summary> public void ReadWritePosDemo(Hasp hasp, HaspFileId fileId) { // sanity check if ((null == hasp) || !hasp.IsLoggedIn()) { return; } Verbose("GetFileSize/FilePos Demo"); // firstly get a file object to a key's file. HaspFile file = hasp.GetFile(fileId); if (!file.IsLoggedIn()) { // Not logged into key - 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; HaspStatus status = file.FileSize(ref size); ReportStatus(status); if (HaspStatus.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 - HaspFile.TypeSize(typeof(int)); // now read what's there int aValue = 0; status = file.Read(ref aValue); ReportStatus(status); if (HaspStatus.StatusOk != status) { Verbose(""); return; } Verbose("Writing to file: 0x" + int.MaxValue.ToString("X2")); // write some data. status = file.Write(int.MaxValue); ReportStatus(status); if (HaspStatus.StatusOk != status) { Verbose(""); return; } // read back the written value. int newValue = 0; Verbose("Reading written data"); status = file.Read(ref newValue); ReportStatus(status); if (HaspStatus.StatusOk == status) { Verbose("Data read: 0x" + newValue.ToString("X2")); } // restore the original data. file.Write(aValue); Verbose(""); }
/// <summary> /// Demonstrates how to perform read and write /// operations on a key's file /// </summary> public void ReadWriteDemo(Hasp hasp, HaspFileId fileId) { // sanity check if ((null == hasp) || !hasp.IsLoggedIn()) { Debug.Log("llllllllllllllllllllll"); return; } Verbose("Read/Write Demo"); // Get a file object to a key's memory file. // please note: the file object is tightly connected // to its key object. logging out from a key also // invalidates the file object. // doing the following will result in an invalid // file object: // hasp.login(...) // HaspFile file = hasp.GetFile(); // hasp.logout(); // Debug.Assert(file.IsValid()); will assert HaspFile file = hasp.GetFile(fileId); if (!file.IsLoggedIn()) { // Not logged into a key - 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; HaspStatus status = file.FileSize(ref size); ReportStatus(status); if (HaspStatus.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 (HaspStatus.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 (HaspStatus.StatusOk != status) { Verbose(""); return; } DumpBytes(newBytes); // and read them again Verbose("Reading written data"); status = file.Read(newBytes, 0, newBytes.Length); ReportStatus(status); if (HaspStatus.StatusOk == status) { DumpBytes(newBytes); } // restore the original contents file.Write(bytes, 0, bytes.Length); Verbose(""); }