private DataManagerWrapper BaseReadXMLContents(string path) { if (!File.Exists(path)) { return(null); } // Creates an instance of the XmlSerializer class; // specifies the type of object to be deserialized. XmlSerializer serializer = new XmlSerializer(typeof(DataManagerWrapper)); // If the XML document has been altered with unknown // nodes or attributes, handles them with the // UnknownNode and UnknownAttribute events. serializer.UnknownNode += new XmlNodeEventHandler(Serializer_UnknownNode); serializer.UnknownAttribute += new XmlAttributeEventHandler(serializer_UnknownAttribute); // A FileStream is needed to read the XML document. FileStream fs = new FileStream(DataPath, FileMode.Open); // Declares an object variable of the type to be deserialized. //List<DriveObject> driveObjects = new List<DriveObject>(); DataManagerWrapper dataWrapper = new DataManagerWrapper(); // Uses the Deserialize method to restore the object's state // with data from the XML document. */ //while(fs.Length != fs.Position) // driveObjects.Add((DriveObject)serializer.Deserialize(fs)); dataWrapper = (DataManagerWrapper)serializer.Deserialize(fs); fs.Close(); return(dataWrapper); }
public void ReadXMLContents() { DataManagerWrapper wrapper = BaseReadXMLContents(DataPath); if (wrapper == null) { DataManager.LogMessage("XMl Read-in failed, wrapper is null"); return; } wrapper.SaveDriveObjects(); // Reads the order date. foreach (DriveObject obj in DataManager.DriveData) { DataManager.LogMessage("Drive Name: " + obj.name); } }
public void SerializeToXML() { // Creates an instance of the XmlSerializer class; // specifies the type of object to serialize. XmlSerializer serializer = new XmlSerializer(typeof(DataManagerWrapper)); TextWriter writer = new StreamWriter(DataPath); // Serializes the drives, and closes the TextWriter. //foreach(DriveObject obj in DataManager.DriveData) //serializer.Serialize(writer, obj); DataManager.DatabaseChanges = true; DataManager.UnsavedChanges = false; DataManagerWrapper dataWrapper = new DataManagerWrapper(); dataWrapper.CacheData(); serializer.Serialize(writer, dataWrapper); writer.Close(); }
public void UploadGoogleDriveData(bool ShowMessages) { // Authorize Google Drive DataManager.LogMessage("Starting Data Upload"); UserCredential credential; using (var stream = new FileStream(CredentialsPath, FileMode.Open, FileAccess.Read)) { // The file token.json stores the user's access and refresh tokens, and is created // automatically when the authorization flow completes for the first time. string credPath = "token.json"; credential = GoogleWebAuthorizationBroker.AuthorizeAsync( GoogleClientSecrets.Load(stream).Secrets, Scopes, "user", CancellationToken.None, new FileDataStore(credPath, true)).Result; DataManager.LogMessage("Credential file saved to: " + credPath); } // Create Drive API service. var service = new DriveService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = ApplicationName, }); service.HttpClient.Timeout = TimeSpan.FromMinutes(100); // Define parameters of request. FilesResource.ListRequest listRequest = service.Files.List(); listRequest.PageSize = 10; listRequest.Fields = "nextPageToken, files(id, name)"; // List files. IList <Google.Apis.Drive.v3.Data.File> files = listRequest.Execute().Files; DataManager.LogMessage("Files:"); string fileID = ""; if (files != null && files.Count > 0) { foreach (var file in files) { DataManager.LogMessage("{0} ({1})", file.Name, file.Id); // Download Drives.xml //file.ExportLinks.Keys. //OutputStream outputStream = new ByteArrayOutputStream(); //driveService.files().get(fileId).executeMediaAndDownloadTo(outputStream); if (file.Name == "Drives.xml" && PreviousMD5 != file.Md5Checksum) { DataManager.LogMessage("Drives.xml found, changes have been made"); fileID = file.Id; // High Probability File has been modified // Download File and incorporate changes before uploading string fileName = "temp" + file.Name; string tempPath = string.Format(@"..\..\{0}", fileName); DataManager.LogMessage(string.Format("Attempting to download Google Drive data to {0}", tempPath)); DownloadFile(service, file, tempPath); if (File.Exists(tempPath)) { DataManager.LogMessage("Temp File Successfully downloaded"); } else { DataManager.LogMessage("Temp File Failed to download"); } DataManagerWrapper wrapper = BaseReadXMLContents(tempPath); DataManager.IncorporateDriveData(wrapper.driveData); File.Delete(tempPath); // Local XMl should be up-to-date with Google Drive XMl } else if (file.Name == "Drives.xml" && PreviousMD5 == file.Md5Checksum) { DataManager.LogMessage("Drives.xml found but no changes by other users have been made"); fileID = file.Id; } } //DataManager.LogMessage("Drives.xml not found in google drive"); } else { DataManager.LogMessage("No files found."); } DataManager.LogMessage("uploading Drives.xml"); var responce = uploadFile(service, DataPath, "", fileID, ShowMessages); DataManager.DatabaseChanges = false; }