static void Main() { var collection = new PortableDeviceCollection(); collection.Refresh(); foreach (var device in collection) { //Console.WriteLine(device.DeviceId); device.Connect(); Console.WriteLine(device.FriendlyName); device.Disconnect(); } Console.WriteLine(); Console.WriteLine("Press any key to continue."); Console.ReadKey(); }
static void Main() { var collection = new PortableDeviceCollection(); collection.Refresh(); Parallel.ForEach(collection, device => { device.Connect(); Console.WriteLine(device.FriendlyName); var folder = device.GetContents(); foreach (var item in folder.Files) { DisplayObject(device, item); } device.Disconnect(); }); }
static void Main() { var devices = new PortableDeviceCollection(); devices.Refresh(); var kindle = devices.First(); kindle.Connect(); kindle.TransferContentToDevice( @"d:\temp\Kindle_Users_Guide.azw", @"g:\documents"); kindle.Disconnect(); Console.WriteLine(); Console.WriteLine("Press any key to continue."); Console.ReadKey(); }
static void Main() { var devices = new PortableDeviceCollection(); devices.Refresh(); var kindle = devices.First(); kindle.Connect(); var root = kindle.GetContents(); foreach (var resource in root.Files) { DisplayResourceContents(resource); } PortableDeviceFolder documents = (from r in ((PortableDeviceFolder)root.Files[0]).Files where r.Name.Contains("documents") select r).First() as PortableDeviceFolder; var books = from b in documents.Files where b.Name.Contains("Kindle_Users_Guide") select b; foreach (PortableDeviceFile book in books) { Console.WriteLine(book.Id); kindle.DeleteFile(book); } kindle.Disconnect(); Console.WriteLine(); Console.WriteLine("Press any key to continue."); Console.ReadKey(); }
static void Main(string[] args) { var devices = new PortableDeviceCollection(); devices.Refresh(); var device = devices.First(); device.Connect(); PortableDeviceFolder folder = device.GetContents(); Console.WriteLine(folder.Name); foreach (PortableDeviceObject obj in folder.Files) { Console.WriteLine(obj.Name); } device.Disconnect(); Console.WriteLine(); Console.WriteLine("Press any key to continue."); Console.ReadKey(); }
static void Main() { var collection = new PortableDeviceCollection(); collection.Refresh(); foreach (var device in collection) { device.Connect(); Console.WriteLine(device.FriendlyName); var folder = device.GetContents(); foreach (var item in folder.Files) { DisplayObject(item); } device.Disconnect(); } Console.WriteLine(); Console.WriteLine("Press any key to continue."); Console.ReadKey(); }
private void btnCopy_Click(object sender, EventArgs e) { string[] drives = Directory.GetLogicalDrives(); bool found = false; foreach (string drive in drives) { if (File.Exists(drive + "Download\\EpiInfo\\Handshake.xml")) { Template template = new Template(mediator); template.CreatePhoneTemplate(drive + "Download\\EpiInfo\\Questionnaires\\" + txtFormName.Text + ".xml"); found = true; } } if (!found) { //try portable device api try { var devices = new PortableDevices.PortableDeviceCollection(); if (devices == null) { Epi.Windows.MsgBox.ShowInformation(SharedStrings.MOBILE_NO_DEVICES); return; } try { devices.Refresh(); } catch (System.IndexOutOfRangeException) { Epi.Windows.MsgBox.ShowInformation(SharedStrings.MOBILE_NO_DEVICES); return; } if (devices.Count == 0) { Epi.Windows.MsgBox.ShowInformation(SharedStrings.MOBILE_NO_DEVICES); return; } var pd = devices.First(); pd.Connect(); PortableDeviceFolder root = pd.GetContents(); PortableDeviceFolder download = (from r in ((PortableDeviceFolder)root.Files[0]).Files where r.Name.Equals("Download") select r).First() as PortableDeviceFolder; PortableDeviceFolder epiinfo = (from r in download.Files where r.Name.Equals("EpiInfo") select r).First() as PortableDeviceFolder; PortableDeviceFolder questionnaires = (from r in epiinfo.Files where r.Name.Equals("Questionnaires") select r).First() as PortableDeviceFolder; if (questionnaires == null) { Epi.Windows.MsgBox.ShowInformation(SharedStrings.MOBILE_NO_DEVICES); pd.Disconnect(); return; } else { try { PortableDeviceFile existingFile = (from r in questionnaires.Files where r.Name.Equals(txtFormName.Text + ".xml") select r).First() as PortableDeviceFile; pd.DeleteFile(existingFile); } catch (Exception ex) { // } Template template = new Template(mediator); template.CreatePhoneTemplate(Path.GetTempPath() + "\\" + txtFormName.Text + ".xml"); pd.TransferContentToDevice(Path.GetTempPath() + "\\" + txtFormName.Text + ".xml", questionnaires.Id); found = true; } pd.Disconnect(); } catch (Exception ex) { } } if (found) { MessageBox.Show(SharedStrings.MOBILE_FORM_COPIED, SharedStrings.MOBILE_FORM_COPY_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); } else { MessageBox.Show(SharedStrings.MOBILE_NO_EI_EXT_FOUND, SharedStrings.ERROR, MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
// https://cgeers.wordpress.com/2012/04/17/wpd-transfer-content-to-a-device/ static void Main() { string error = string.Empty; PortableDeviceCollection devices = null; try { devices = new PortableDeviceCollection(); if (null != devices) { char cmdCharacter = ' '; do { devices.Refresh(); Console.Clear(); Console.WriteLine($"Found {devices.Count} Device(s)"); Console.WriteLine($"-------------------------------------"); foreach (var device in devices) { Console.WriteLine(); Console.WriteLine($"Found Device {device.Name} with ID {device.DeviceId}"); Console.WriteLine($"\tManufacturer: {device.Manufacturer}"); Console.WriteLine($"\tDescription: {device.Description}"); device.Connect(); var rootfolder = device.Root; Console.WriteLine($"Root Folder {rootfolder.Name}"); IPortableDeviceContent content = device.getContents(); // list all contents in the root - 1 level in //see GetFiles method to enumerate everything in the device PortableDeviceFolder.EnumerateContents(ref content, rootfolder); foreach (var fileItem in rootfolder.Files) { Console.WriteLine($"\t{fileItem.Name}"); if (fileItem is PortableDeviceFolder childFolder) { PortableDeviceFolder.EnumerateContents(ref content, childFolder); foreach (var childFile in childFolder.Files) { Console.WriteLine($"\t\t{childFile.Name}"); } } } // Copy folder to device from pc. //error = copyToDevice (device); //if (String.IsNullOrEmpty(error)) //{ // error = @"Copied folder C:\Test to Phone\Android\data\test"; //} //Console.WriteLine(error); //// Copy folder back to pc from device. //error = copyFromDevice(device); //if (String.IsNullOrEmpty(error)) //{ // error = @"Copied folder Phone\Android\data\test to c:\Test\CopiedBackfromPhone"; //} device.Disconnect(); } Console.WriteLine($"-------------------------------------"); Console.WriteLine("Press r to refresh"); Console.WriteLine("Press x key to exit"); cmdCharacter = Console.ReadKey().KeyChar; if (cmdCharacter == 'x') { break; } } while ('r' == cmdCharacter); } } catch (Exception ex) { error = ex.Message; } finally { if (null != devices) { devices.Dispose(); } if (!string.IsNullOrWhiteSpace(error)) { Console.WriteLine(error); Console.ReadKey(); } } }
/// <summary> /// Processes all of the fields on a given form, page-by-page, except for the fields on the base table. /// </summary> /// <param name="destinationView">The destination form</param> /// <param name="destinationGUIDList">The list of GUIDs that exist in the destination</param> private void ProcessPages(View destinationView, List<string> destinationGUIDList, string syncFilePath) { int recordsProcessed = 0; foreach (KeyValuePair<string, List<PhoneFieldData>> kvp in _surveyResponses) { string recordKey = kvp.Key; Dictionary<int, List<PhoneFieldData>> pagedFieldDataDictionary = new Dictionary<int, List<PhoneFieldData>>(); for (int i = 0; i < destinationView.Pages.Count; i++) { pagedFieldDataDictionary.Add(destinationView.Pages[i].Id, new List<PhoneFieldData>()); } foreach (PhoneFieldData fieldData in kvp.Value) { int pageId = fieldData.Page; if (fieldData.FieldName.ToLower().Equals("globalrecordid") || destinationView.GetPageById(pageId).Fields.Contains(fieldData.FieldName)) { pagedFieldDataDictionary[pageId].Add(fieldData); } } for (int i = 0; i < destinationView.Pages.Count; i++) { this.BeginInvoke(new SetStatusDelegate(SetStatusMessage), "Processing records on page " + (i + 1).ToString() + " of " + destinationView.Pages.Count.ToString() + "..."); int recordsInserted = 0; int recordsUpdated = 0; Page destinationPage = destinationView.Pages[i]; string pageTableName = destinationPage.TableName; List<PhoneFieldData> pagedFieldDataList = pagedFieldDataDictionary[destinationPage.Id]; Query selectQuery = destinationProjectDataDriver.CreateQuery("SELECT [GlobalRecordId] FROM [" + pageTableName + "]"); IDataReader destReader = destinationProjectDataDriver.ExecuteReader(selectQuery); destinationGUIDList = new List<string>(); while (destReader.Read()) { destinationGUIDList.Add(destReader[0].ToString()); } destReader.Close(); try { if (pagedFieldDataList.Count == 0 && append) { InsertEmptyPageTableRecord(recordKey, pageTableName); } string currentGUID = string.Empty; string lastGUID = string.Empty; WordBuilder fieldNames = new WordBuilder(StringLiterals.COMMA); WordBuilder fieldValues = new WordBuilder(StringLiterals.COMMA); List<QueryParameter> fieldValueParams = new List<QueryParameter>(); int fieldsInQuery = 0; PhoneFieldData wfData = new PhoneFieldData(); wfData.RecordGUID = "__LAST__"; // last field, acts as a flag pagedFieldDataList.Add(wfData); List<string> GUIDList = new List<string>(); foreach (PhoneFieldData fieldData in pagedFieldDataList) { currentGUID = fieldData.RecordGUID; if (importWorker.CancellationPending) { this.BeginInvoke(new SetStatusDelegate(AddStatusMessage), "Import cancelled."); return; } string GUID = fieldData.RecordGUID; // sourceReader["GlobalRecordId"].ToString(); List<string> updatedGUIDs = new List<string>(); if (destinationGUIDList.Contains(GUID) && update) { #region UPDATE currentGUID = fieldData.RecordGUID; if (importWorker.CancellationPending) { this.BeginInvoke(new SetStatusDelegate(AddStatusMessage), "Import cancelled."); return; } string updateHeader = string.Empty; string whereClause = string.Empty; fieldValueParams = new List<QueryParameter>(); StringBuilder sb = new StringBuilder(); // Build the Update statement which will be reused sb.Append(SqlKeyWords.UPDATE); sb.Append(StringLiterals.SPACE); sb.Append(destinationProjectDataDriver.InsertInEscape(destinationPage.TableName)); sb.Append(StringLiterals.SPACE); sb.Append(SqlKeyWords.SET); sb.Append(StringLiterals.SPACE); updateHeader = sb.ToString(); sb.Remove(0, sb.ToString().Length); // Build the WHERE caluse which will be reused sb.Append(SqlKeyWords.WHERE); sb.Append(StringLiterals.SPACE); sb.Append(destinationProjectDataDriver.InsertInEscape(ColumnNames.GLOBAL_RECORD_ID)); sb.Append(StringLiterals.EQUAL); sb.Append("'"); sb.Append(GUID); sb.Append("'"); whereClause = sb.ToString(); sb.Remove(0, sb.ToString().Length); sb.Append(StringLiterals.LEFT_SQUARE_BRACKET); sb.Append(fieldData.FieldName); sb.Append(StringLiterals.RIGHT_SQUARE_BRACKET); sb.Append(StringLiterals.EQUAL); sb.Append(StringLiterals.COMMERCIAL_AT); sb.Append(fieldData.FieldName); QueryParameter param = GetQueryParameterForField(fieldData, destinationPage, syncFilePath); if (param != null) { Query updateQuery = destinationProjectDataDriver.CreateQuery(updateHeader + StringLiterals.SPACE + sb.ToString() + StringLiterals.SPACE + whereClause); updateQuery.Parameters.Add(param); destinationProjectDataDriver.ExecuteNonQuery(updateQuery); if (!GUIDList.Contains(GUID)) { GUIDList.Add(GUID); recordsUpdated++; this.BeginInvoke(new SetProgressBarDelegate(IncrementProgressBarValue), 1); } } #endregion // UPDATE } else if (!destinationGUIDList.Contains(GUID) && append) { #region APPEND if (!string.IsNullOrEmpty(lastGUID) && lastGUID != currentGUID) { // Concatenate the query clauses into one SQL statement. StringBuilder sb = new StringBuilder(); sb.Append(" insert into "); sb.Append(destinationProjectDataDriver.InsertInEscape(destinationPage.TableName)); sb.Append(StringLiterals.SPACE); sb.Append(Util.InsertInParantheses(fieldNames.ToString())); sb.Append(" values ("); sb.Append(fieldValues.ToString()); sb.Append(") "); Query insertQuery = destinationProjectDataDriver.CreateQuery(sb.ToString()); insertQuery.Parameters = fieldValueParams; destinationProjectDataDriver.ExecuteNonQuery(insertQuery); recordsInserted++; fieldNames = new WordBuilder(StringLiterals.COMMA); fieldValues = new WordBuilder(StringLiterals.COMMA); fieldValueParams = new List<QueryParameter>(); fieldsInQuery = 0; } lastGUID = fieldData.RecordGUID; if (lastGUID == "__LAST__") { break; } if (!fieldNames.Contains("GlobalRecordId")) { fieldNames.Append("GlobalRecordId"); fieldValues.Append("@GlobalRecordId"); fieldValueParams.Add(new QueryParameter("@GlobalRecordId", DbType.String, GUID)); fieldsInQuery++; } Field dataField = destinationView.Fields[fieldData.FieldName]; // already checked for this above so should never fail... if (!( dataField is GroupField || dataField is RelatedViewField || dataField is UniqueKeyField || dataField is RecStatusField || dataField is GlobalRecordIdField || fieldData.FieldValue == null || string.IsNullOrEmpty(fieldData.FieldValue.ToString()) )) { String fieldName = ((Epi.INamedObject)dataField).Name; switch (dataField.FieldType) { case MetaFieldType.Date: case MetaFieldType.DateTime: case MetaFieldType.Time: fieldValueParams.Add(new QueryParameter("@" + fieldName, DbType.DateTime, Convert.ToDateTime(fieldData.FieldValue))); break; case MetaFieldType.Checkbox: fieldValueParams.Add(new QueryParameter("@" + fieldName, DbType.Boolean, Convert.ToBoolean(fieldData.FieldValue))); break; case MetaFieldType.CommentLegal: case MetaFieldType.LegalValues: case MetaFieldType.Codes: case MetaFieldType.Text: case MetaFieldType.TextUppercase: case MetaFieldType.PhoneNumber: case MetaFieldType.UniqueRowId: case MetaFieldType.ForeignKey: case MetaFieldType.GlobalRecordId: case MetaFieldType.Multiline: fieldValueParams.Add(new QueryParameter("@" + fieldName, DbType.String, fieldData.FieldValue)); break; case MetaFieldType.Number: case MetaFieldType.YesNo: case MetaFieldType.Option: case MetaFieldType.RecStatus: fieldValueParams.Add(new QueryParameter("@" + fieldName, DbType.Single, fieldData.FieldValue)); break; case MetaFieldType.Image: byte[] imageBytes = null; if (syncFilePath.Contains("EpiInfo\\")) { string part1 = syncFilePath.Substring(0, syncFilePath.IndexOf("EpiInfo")); string part2 = fieldData.FieldValue.ToString().Substring(fieldData.FieldValue.ToString().IndexOf("EpiInfo"), fieldData.FieldValue.ToString().Length - fieldData.FieldValue.ToString().IndexOf("EpiInfo")); string fileName = part1 + part2; if (File.Exists(fileName)) { imageBytes = Util.GetByteArrayFromImagePath(fileName); } } else { var devices = new PortableDevices.PortableDeviceCollection(); try { devices.Refresh(); } catch (System.IndexOutOfRangeException) { break; } if (devices.Count == 0) { break; } var pd = devices.First(); pd.Connect(); PortableDeviceFolder root = pd.GetContents(); PortableDeviceFolder download = (from r in ((PortableDeviceFolder)root.Files[0]).Files where r.Name.Equals("Download") select r).First() as PortableDeviceFolder; PortableDeviceFolder epiinfo = (from r in download.Files where r.Name.Equals("EpiInfo") select r).First() as PortableDeviceFolder; PortableDeviceFolder images = (from r in epiinfo.Files where r.Name.Equals("Images") select r).First() as PortableDeviceFolder; if (images == null) { pd.Disconnect(); break; } else { try { string imageFileName = fieldData.FieldValue.ToString().Substring(fieldData.FieldValue.ToString().IndexOf("Images/")).Split('/')[1].Split('.')[0]; string tempPath = Path.GetTempPath(); PortableDeviceFile existingFile = (from r in images.Files where r.Name.Equals(imageFileName) select r).First() as PortableDeviceFile; new FileInfo(tempPath + existingFile.Id).Create().Close(); pd.DownloadFile(existingFile, tempPath); imageBytes = Util.GetByteArrayFromImagePath(tempPath + existingFile.Id); } catch (Exception ex) { // } } } if (imageBytes != null) { fieldValueParams.Add(new QueryParameter("@" + fieldName, DbType.Binary, imageBytes)); } break; default: throw new ApplicationException("Not a supported field type"); } fieldNames.Append(destinationProjectDataDriver.InsertInEscape(((Epi.INamedObject)dataField).Name)); fieldValues.Append("@" + fieldName); fieldsInQuery++; } #endregion // APPEND } this.BeginInvoke(new SetProgressBarDelegate(IncrementProgressBarValue), 1); } } catch (Exception ex) { this.BeginInvoke(new SetStatusDelegate(AddErrorStatusMessage), ex.Message); } finally { } // this.BeginInvoke(new SetStatusDelegate(AddStatusMessage), "On page '" + destinationPage.Name + "', " + recordsInserted.ToString() + " record(s) inserted and " + recordsUpdated.ToString() + " record(s) updated."); } recordsProcessed++; } this.BeginInvoke(new SetStatusDelegate(AddStatusMessage), recordsProcessed.ToString() + " record(s) processed."); }
private QueryParameter GetQueryParameterForField(PhoneFieldData fieldData, Page sourcePage, string syncFilePath) { Field dataField = destinationView.Fields[fieldData.FieldName]; if (!( dataField is GroupField || dataField is RelatedViewField || dataField is UniqueKeyField || dataField is RecStatusField || dataField is GlobalRecordIdField || fieldData.FieldValue == null || string.IsNullOrEmpty(fieldData.FieldValue.ToString()) )) { String fieldName = ((Epi.INamedObject)dataField).Name; switch (dataField.FieldType) { case MetaFieldType.Date: case MetaFieldType.DateTime: case MetaFieldType.Time: return new QueryParameter("@" + fieldName, DbType.DateTime, Convert.ToDateTime(fieldData.FieldValue)); case MetaFieldType.Checkbox: return new QueryParameter("@" + fieldName, DbType.Boolean, Convert.ToBoolean(fieldData.FieldValue)); case MetaFieldType.CommentLegal: case MetaFieldType.LegalValues: case MetaFieldType.Codes: case MetaFieldType.Text: case MetaFieldType.TextUppercase: case MetaFieldType.PhoneNumber: case MetaFieldType.UniqueRowId: case MetaFieldType.ForeignKey: case MetaFieldType.GlobalRecordId: case MetaFieldType.Multiline: return new QueryParameter("@" + fieldName, DbType.String, fieldData.FieldValue); case MetaFieldType.Number: case MetaFieldType.YesNo: case MetaFieldType.Option: case MetaFieldType.RecStatus: return new QueryParameter("@" + fieldName, DbType.Single, fieldData.FieldValue); case MetaFieldType.Image: byte[] imageBytes = null; if (syncFilePath.Contains("EpiInfo\\")) { string part1 = syncFilePath.Substring(0, syncFilePath.IndexOf("EpiInfo")); string part2 = fieldData.FieldValue.ToString().Substring(fieldData.FieldValue.ToString().IndexOf("EpiInfo"), fieldData.FieldValue.ToString().Length - fieldData.FieldValue.ToString().IndexOf("EpiInfo")); string fileName = part1 + part2; if (File.Exists(fileName)) { imageBytes = Util.GetByteArrayFromImagePath(fileName); } } else { imageBytes = null; var devices = new PortableDevices.PortableDeviceCollection(); try { devices.Refresh(); } catch (System.IndexOutOfRangeException) { break; } if (devices.Count == 0) { break; } var pd = devices.First(); pd.Connect(); PortableDeviceFolder root = pd.GetContents(); PortableDeviceFolder download = (from r in ((PortableDeviceFolder)root.Files[0]).Files where r.Name.Equals("Download") select r).First() as PortableDeviceFolder; PortableDeviceFolder epiinfo = (from r in download.Files where r.Name.Equals("EpiInfo") select r).First() as PortableDeviceFolder; PortableDeviceFolder images = (from r in epiinfo.Files where r.Name.Equals("Images") select r).First() as PortableDeviceFolder; if (images == null) { pd.Disconnect(); break; } else { try { string imageFileName = fieldData.FieldValue.ToString().Substring(fieldData.FieldValue.ToString().IndexOf("Images/")).Split('/')[1].Split('.')[0]; string tempPath = Path.GetTempPath(); PortableDeviceFile existingFile = (from r in images.Files where r.Name.Equals(imageFileName) select r).First() as PortableDeviceFile; new FileInfo(tempPath + existingFile.Id).Create().Close(); pd.DownloadFile(existingFile, tempPath); imageBytes = Util.GetByteArrayFromImagePath(tempPath + existingFile.Id); } catch (Exception ex) { // } } } if (imageBytes != null) { return new QueryParameter("@" + fieldName, DbType.Binary, imageBytes); } else { return new QueryParameter("@" + fieldName, DbType.Binary, null); } default: throw new ApplicationException("Not a supported field type"); } } return null; }