/// <summary> /// Copies a file from the remote device to the local system. /// </summary> /// <param name="device">The device.</param> /// <param name="deviceFileName">The name of the remote file to copy.</param> /// <param name="desktopFileName">The name of the local destination file.</param> /// <param name="overwrite">true if the destination file can be overwritten; otherwise, false.</param> public static void CopyFileFromDevice(RemoteDevice device, string deviceFileName, string desktopFileName, bool overwrite) { using var remoteFile = new RemoteDevice.DeviceFile(device.ISession, deviceFileName, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL); using var localFile = new FileStream(desktopFileName, overwrite ? FileMode.Create : FileMode.CreateNew, FileAccess.Write); // read data from remote file into buffer byte[] buffer = new byte[FileBufferSize]; int bytesread = 0; while ((bytesread = remoteFile.Read(buffer, 0, FileBufferSize)) > 0) { // write it into local file localFile.Write(buffer, 0, bytesread); } }
/// <summary> /// Reads the contents of a file on a remote device into a array of bytes. /// </summary> /// <param name="device">The device.</param> /// <param name="path">A file to open for reading.</param> /// <returns> /// A byte array containing the contents of the file. /// </returns> public static byte[] ReadAllBytes(RemoteDevice device, string path) { byte[] ret = new byte[0]; using (RemoteDevice.DeviceFile remoteFile = new RemoteDevice.DeviceFile(device.ISession, path, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL)) { using MemoryStream mem = new MemoryStream(10240); // read data from remote file into buffer byte[] buffer = new byte[FileBufferSize]; int bytesread = 0; while ((bytesread = remoteFile.Read(buffer, 0, FileBufferSize)) > 0) { // write it into local file mem.Write(buffer, 0, bytesread); } ret = mem.ToArray(); } return(ret); }
/// <summary> /// Initializes a new instance of the <see cref="RemoteFileStream"/> class with the specified path, creation mode, read/write permission, sharing permission, and attributes. /// </summary> /// <param name="device">The device.</param> /// <param name="path">A relative or absolute path for the file that the current <see cref="RemoteFileStream"/> object will encapsulate.</param> /// <param name="mode">A <see cref="FileMode"/> value that specifies whether a file is created if one does not exist, and determines whether the contents of existing files are retained or overwritten.</param> /// <param name="access">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the file.</param> /// <param name="share">A <see cref="FileShare"/> value specifying the type of access other threads have to the file.</param> /// <param name="attributes">The <see cref="FileAttributes"/> to set on the new file.</param> public RemoteFileStream(RemoteDevice device, string path, FileMode mode, FileAccess access, FileShare share, FileAttributes attributes) { f = new RemoteDevice.DeviceFile(device.ISession, path, ((uint)access << 30), (uint)share, (uint)mode, (uint)attributes); _canRead = (access & FileAccess.Read) != 0; _canWrite = (access & FileAccess.Write) != 0; _canSeek = true; _pos = 0L; _bufferSize = DefaultBufferSize; _readPos = 0; _readLen = 0; _writePos = 0; if (mode == FileMode.Append) { _appendStart = SeekCore(0L, SeekOrigin.End); } else { _appendStart = -1L; } }
/// <summary> /// Copies a file from the local system to a remote device. /// </summary> /// <param name="device">The device.</param> /// <param name="desktopFileName">The name of the local file to copy.</param> /// <param name="deviceFileName">The name of the remote destination file.</param> /// <param name="overwrite"><see langword="true"/> if the destination file can be overwritten; otherwise, <see langword="false"/>.</param> /// <param name="percentProgressCallback">Optional. A callback delegate to receive progress updates. This value can be <see langword="null"/>.</param> public static void CopyFileToDevice(RemoteDevice device, string desktopFileName, string deviceFileName, bool overwrite, Action <float> percentProgressCallback) { using var remoteFile = new RemoteDevice.DeviceFile(device.ISession, deviceFileName, GENERIC_WRITE, FILE_SHARE_READ, overwrite ? CREATE_ALWAYS : CREATE_NEW, FILE_ATTRIBUTE_NORMAL); using (var localFile = new FileStream(desktopFileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { float totalSize = localFile.Length; long filepos = 0; byte[] buffer = new byte[FileBufferSize]; int bytesread = localFile.Read(buffer, 0, buffer.Length); while (bytesread > 0) { // move remote file pointer # of bytes read filepos += bytesread; // write our buffer to the remote file remoteFile.Write(buffer, 0, bytesread); // notify of progress, if elected if (percentProgressCallback != null) { // use BeginInvoke as a "fire and forget" notification, to help prevent deadlocks percentProgressCallback.BeginInvoke(filepos / totalSize, null, null); } try { // refill the local buffer bytesread = localFile.Read(buffer, 0, buffer.Length); } catch (Exception) { bytesread = 0; } } remoteFile.SetEndOfFile(); } remoteFile.SetFileTimes(File.GetCreationTime(desktopFileName), File.GetLastAccessTime(desktopFileName), File.GetLastWriteTime(desktopFileName)); }
/// <summary> /// Sets the date and time that a file was created, last accessed, or last modified for files on a remote device. /// </summary> /// <param name="device">The device.</param> /// <param name="fileName">String that specifies the name of a file.</param> /// <param name="creationTime">The date and time the file was created. This parameter can be NULL if the application does not need to set this information.</param> /// <param name="lastAccessTime">The date and time the file was last accessed. This parameter can be NULL if the application does not need to set this information.</param> /// <param name="lastWriteTime">The date and time the file was last modified. This parameter can be NULL if the application does not need to set this information.</param> internal static void SetFileTimes(RemoteDevice device, string fileName, DateTime?creationTime, DateTime?lastAccessTime, DateTime?lastWriteTime) { using RemoteDevice.DeviceFile f = new RemoteDevice.DeviceFile(device.ISession, fileName); f.SetFileTimes(creationTime, lastAccessTime, lastWriteTime); }
/// <summary> /// Gets the date and time the specified file or directory was last written to. /// </summary> /// <param name="device">The device.</param> /// <param name="path">The file or directory for which to obtain access date and time information.</param> /// <returns>A <see cref="DateTime"/> structure set to the date and time that the specified file or directory was last written to. This value is expressed in local time.</returns> public static DateTime GetLastWriteTime(RemoteDevice device, string path) { using RemoteDevice.DeviceFile f = new RemoteDevice.DeviceFile(device.ISession, path); return(f.GetFileTimes().LastWriteTime); }
/// <summary> /// Sets the file times. /// </summary> /// <param name="creationTime">The creation time.</param> /// <param name="lastAccessTime">The last access time.</param> /// <param name="lastWriteTime">The last write time.</param> internal void SetFileTimes(DateTime?creationTime, DateTime?lastAccessTime, DateTime?lastWriteTime) { using RemoteDevice.DeviceFile f = new RemoteDevice.DeviceFile(Device.ISession, FullPath); f.SetFileTimes(creationTime, lastAccessTime, lastWriteTime); }
/// <summary> /// Refreshes the state of the object. /// </summary> public void Refresh() { using RemoteDevice.DeviceFile f = new RemoteDevice.DeviceFile(Device.ISession, FullPath); ftimes = f.GetFileTimes(); }