PDAclub.pl - Forum użytkowników technologii mobilnych

Windows Mobile (Classic, Professional, Standard), Windows Phone 6.x oraz 7/8.x/10 => Oprogramowanie PPC => Dla programistów => Wątek zaczęty przez: tzok w Styczeń 21, 2009, 17:29:57

Tytuł: Przesyłanie pliku PocketPC->PC z poziomu C#
Wiadomość wysłana przez: tzok w Styczeń 21, 2009, 17:29:57
Potrzebuję przesłać pewną ilość informacji z programu działającego na PocketPC (WM5) do programu działającego na PC. Oba programy mają być napisane w C# (VS 2008), a połączenie będzie realizowane przez ActiveSync. Jak najprościej przesłać te dane (tablica 2D - kilkaset wierszy, 4 kolumny), może być przesłane przez plik .txt, może być jakoś inaczej...
Tytuł: Odp: Przesyłanie pliku PocketPC->PC z poziomu C#
Wiadomość wysłana przez: wally w Styczeń 22, 2009, 15:02:48
Łatwiej będzie aby to aplikacja PC pobierała plik. Do tego celu użyj biblioteki "OpenNETCF.Desktop.Communication.dll" i klasy RAPI lub:

using System;
using System.Collections.Generic;
using System.Text;
using OpenNETCF.Desktop.Communication;
using System.IO;
using System.Threading;

namespace Wally.PDA.TransferPlikow
{
    public class Psion : IDisposable
    {
        RAPI m_rapi;

        public bool IsConnected
        {
            get
            {
                return m_rapi.Connected;
            }
        }

        public event EventHandler<ErrorEventArgs> Error;
        void OnError(Exception e)
        {
            if (null != Error)
            {
                Error(this, new ErrorEventArgs(e));
            }
        }

        public event RAPIConnectedHandler Connected
        {
            add
            {
                m_rapi.RAPIConnected += value;
            }
            remove
            {
                m_rapi.RAPIConnected -= value;
            }
        }

        public event RAPIConnectedHandler Disconnected
        {
            add
            {
                m_rapi.RAPIDisconnected += value;
            }
            remove
            {
                m_rapi.RAPIDisconnected -= value;
            }
        }

        public Psion()
        {
            this.m_rapi = new RAPI();
        }

        public void Connect()
        {
            ThreadStart threadStart = new ThreadStart(TryConnect);
            Thread thread = new Thread(threadStart);
            thread.Start();
            thread.Join(6000);
            if (thread.IsAlive)
            {
                thread.Abort();
                thread.Join(6000);
                OnError(new Exception("Nie udało się połączyć z urządzeniem"));
            }
        }

        void TryConnect()
        {
            this.m_rapi.Connect(true, 1);
            //this.m_rapi.Connect(false);
        }

        public void Disconnect()
        {
            this.m_rapi.Disconnect();
        }

        public FileList GetFiles(string fileName)
        {
            return m_rapi.EnumFiles(fileName);
        }

        public bool IsFile(string file)
        {
            OpenNETCF.Desktop.Communication.RAPI.RAPIFileAttributes fa = m_rapi.GetDeviceFileAttributes(file);
            return RAPI.RAPIFileAttributes.Directory != (fa & RAPI.RAPIFileAttributes.Directory);
        }

        public bool IsFile(FileInformation fi)
        {
            switch (fi.FileAttributes)
            {
                case 272:
                    return false;
                case 16:
                case 18:
                case 20:
                    return false;
                default:
                    return true;
            }
        }

        public bool IsFolder(FileInformation fi)
        {
            switch (fi.FileAttributes)
            {
                case 272:
                    return false;
                case 16:
                case 18:
                case 20:
                    return true;
                default:
                    return false;
            }
        }

        public bool CopyFromDevice(string[] files, string path)
        {
            foreach (string file in files)
            {
                if (IsFile(file))
                {
                    try
                    {
                        if (System.IO.File.Exists(System.IO.Path.Combine(path, System.IO.Path.GetFileName(file))))
                        {
                            System.IO.File.Delete(System.IO.Path.Combine(path, System.IO.Path.GetFileName(file)));
                        }
                        m_rapi.CopyFileFromDevice(System.IO.Path.Combine(path, System.IO.Path.GetFileName(file)), file);
                    }
                    catch //(Exception e)
                    {
                        OnError(new Exception(string.Format("Problem z zapisem pliku '{0}' do '{1}'", file, System.IO.Path.Combine(path, System.IO.Path.GetFileName(file)))));
                        return false;
                    }
                }
                else
                {
                    if (!System.IO.Directory.Exists(System.IO.Path.Combine(path, System.IO.Path.GetFileName(file))))
                    {
                        System.IO.Directory.CreateDirectory(System.IO.Path.Combine(path, System.IO.Path.GetFileName(file)));
                    }
                    List<string> list = new List<string>();
                    FileList fl = m_rapi.EnumFiles(System.IO.Path.Combine(file, "*.*"));
                    if (null != fl)
                    {
                        foreach (FileInformation fi in fl)
                        {
                            list.Add(System.IO.Path.Combine(file, fi.FileName));
                        }
                        if (!CopyFromDevice(list.ToArray(), System.IO.Path.Combine(path, System.IO.Path.GetFileName(file))))
                        {
                            return false;
                        }
                    }
                }
            }
            return true;
        }

        public bool CopyToDevice(string[] files, string path)
        {
            foreach (string file in files)
            {
                if (System.IO.File.Exists(file))
                {
                    try
                    {
                        if (FileExists(System.IO.Path.Combine(path, System.IO.Path.GetFileName(file))))
                        {   //usuwanie pliku jesli istnial na urzadzeniu
                            DeleteDeviceFile(new string[] { System.IO.Path.Combine(path, System.IO.Path.GetFileName(file)) });
                        }
                        m_rapi.CopyFileToDevice(file, System.IO.Path.Combine(path, System.IO.Path.GetFileName(file)));
                    }
                    catch
                    {
                        OnError(new Exception(string.Format("Problem z zapisem pliku '{0}' do '{1}'", file, System.IO.Path.Combine(path, System.IO.Path.GetFileName(file)))));
                        return false;
                    }
                }
                else
                {
                    if (!m_rapi.DeviceFileExists(path))
                    {
                        m_rapi.CreateDeviceDirectory(path);
                    }
                    if (!m_rapi.DeviceFileExists(System.IO.Path.Combine(path, System.IO.Path.GetFileName(file))))
                    {
                        m_rapi.CreateDeviceDirectory(System.IO.Path.Combine(path, System.IO.Path.GetFileName(file)));
                    }
                    if (!CopyToDevice(System.IO.Directory.GetFiles(file, "*.*", SearchOption.TopDirectoryOnly), System.IO.Path.Combine(path, System.IO.Path.GetFileName(file))) ||
                        !CopyToDevice(System.IO.Directory.GetDirectories(file, "*.*", SearchOption.TopDirectoryOnly), System.IO.Path.Combine(path, System.IO.Path.GetFileName(file))))
                    {
                        return false;
                    }
                }
            }
            return true;
        }

        public void DeleteDeviceFile(string[] files)
        {
            foreach (string file in files)
            {
                if (IsFile(file))
                {
                    try
                    {
                        m_rapi.DeleteDeviceFile(file);
                    }
                    catch
                    {
                        OnError(new Exception(string.Format("Problem z usuwaniem pliku '{0}'", file)));
                        return;
                    }
                }
                else
                {
                    try
                    {
                        m_rapi.RemoveDeviceDirectory(file, true);
                    }
                    catch
                    {
                        OnError(new Exception(string.Format("Problem z usuwaniem folderu '{0}'", file)));
                        return;
                    }
                }
            }
        }

        public bool FileExists(string fileName)
        {
            return m_rapi.DeviceFileExists(fileName);
        }

        public void CreateDirectory(string dir)
        {
            for (int i=0; i < dir.Length; i++)
            {
                int index = dir.IndexOf('\\', i);
                if (-1 != index)
                {
                    string d = dir.Substring(0, index);
                    if (!FileExists(d))
                    {
                        m_rapi.CreateDeviceDirectory(d);
                    }
                }
                else
                {
                    m_rapi.CreateDeviceDirectory(dir);
                    break;
                }
                i=index;
            }
        }

        #region IDisposable Members

        public void Dispose()
        {
            this.m_rapi.Dispose();
            this.m_rapi = null;
        }

        #endregion
    }
}