public static bool HandleNextCommand(this UsbK USB) { bool ok = true; CommandReadResult rres = USB.ReadCommandInput(out CommandId cmdid); if (rres == CommandReadResult.Success) { Program.Command.LogL("Received request (Command: " + cmdid + ")"); switch (cmdid) { case CommandId.ListSystemDrives: { var drives = DriveInfo.GetDrives(); List <DriveInfo> adrives = new List <DriveInfo>(); uint count = (uint)drives.Length; if (drives.Length > 0) { foreach (var drive in drives) { if (drive.IsReady) { adrives.Add(drive); } } } USB.Write32((uint)adrives.Count); if (adrives.Count > 0) { foreach (var drive in adrives) { string label = drive.VolumeLabel; string prefix = drive.Name.Substring(0, 1); USB.WriteString(label); USB.WriteString(prefix); } } break; } case CommandId.GetEnvironmentPaths: { var envpaths = new Dictionary <string, Environment.SpecialFolder>() { { "Desktop", Environment.SpecialFolder.Desktop }, { "Documents", Environment.SpecialFolder.MyDocuments }, }; USB.Write32((uint)envpaths.Count); foreach (var path in envpaths) { string pth = Environment.GetFolderPath(path.Value); string spth = pth.NormalizeAsGoldleafPath(); USB.WriteString(path.Key); USB.WriteString(spth); } break; } case CommandId.GetPathType: { string spath = USB.ReadString(); string path = spath.NormalizeAsPath(); uint ptype = 0; if (File.Exists(path)) { ptype = 1; } else if (Directory.Exists(path)) { ptype = 2; } USB.Write32(ptype); break; } case CommandId.ListDirectories: { string spath = USB.ReadString(); string path = spath.NormalizeAsPath(); var ents = Directory.GetDirectories(path); USB.Write32((uint)ents.Length); if (ents.Length > 0) { foreach (var ent in ents) { string name = Path.GetFileName(ent); USB.WriteString(name); } } break; } case CommandId.ListFiles: { string spath = USB.ReadString(); string path = spath.NormalizeAsPath(); var ents = Directory.GetFiles(path); USB.Write32((uint)ents.Length); if (ents.Length > 0) { foreach (var ent in ents) { string name = Path.GetFileName(ent); USB.WriteString(name); } } break; } case CommandId.GetFileSize: { string spath = USB.ReadString(); string path = spath.NormalizeAsPath(); ulong size = 0; try { size = (ulong)new FileInfo(path).Length; } catch { size = 0; } USB.Write64(size); break; } case CommandId.FileRead: { ulong offset = USB.Read64(); ulong size = USB.Read64(); string spath = USB.ReadString(); string path = spath.NormalizeAsPath(); ulong rbytes = 0; byte[] data = new byte[size]; try { using (BinaryReader reader = new BinaryReader(new FileStream(path, FileMode.Open, FileAccess.Read))) { reader.BaseStream.Seek((long)offset, SeekOrigin.Begin); rbytes = (ulong)reader.Read(data, 0, (int)size); } } catch { } // Console.WriteLine("FileRead - Offset: " + offset + ", RBytes: " + rbytes); USB.Write64(rbytes); if (rbytes > 0) { USB.WriteBytes(data); } break; } case CommandId.FileWrite: { ulong offset = USB.Read64(); ulong size = USB.Read64(); string spath = USB.ReadString(); byte[] data = USB.ReadBytes((int)size); string path = spath.NormalizeAsPath(); try { var fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write) { Position = (long)offset, }; fs.Write(data, 0, (int)size); fs.Close(); } catch { } break; } case CommandId.CreateFile: { string spath = USB.ReadString(); string path = spath.NormalizeAsPath(); File.Create(path); break; } case CommandId.CreateDirectory: { string spath = USB.ReadString(); string path = spath.NormalizeAsPath(); Directory.CreateDirectory(path); break; } case CommandId.DeleteFile: { string spath = USB.ReadString(); string path = spath.NormalizeAsPath(); File.Delete(path); break; } case CommandId.DeleteDirectory: { string spath = USB.ReadString(); string path = spath.NormalizeAsPath(); Directory.Delete(path, true); break; } case CommandId.RenameFile: { string spath = USB.ReadString(); string newname = USB.ReadString(); string path = spath.NormalizeAsPath(); string npath = Path.Combine(Path.GetDirectoryName(path), newname); File.Move(path, npath); break; } case CommandId.RenameDirectory: { string spath = USB.ReadString(); string newname = USB.ReadString(); string path = spath.NormalizeAsPath(); string npath = Path.Combine(Path.GetDirectoryName(path), newname); Directory.Move(path, npath); break; } case CommandId.GetDriveTotalSpace: { string sdrive = USB.ReadString(); var drives = DriveInfo.GetDrives(); ulong space = 0; if (drives.Length > 0) { foreach (var drive in drives) { if (drive.IsReady && (drive.Name == sdrive)) { space = (ulong)drive.TotalSize; break; } } } USB.Write64(space); break; } case CommandId.GetDriveFreeSpace: { string sdrive = USB.ReadString(); var drives = DriveInfo.GetDrives(); ulong space = 0; if (drives.Length > 0) { foreach (var drive in drives) { if (drive.IsReady && (drive.Name == sdrive)) { space = (ulong)drive.TotalFreeSpace; break; } } } USB.Write64(space); break; } } } else { ok = false; } return(ok); }
public static CommandHandleResult HandleNextCommand(this UsbK USB) { CommandHandleResult res = CommandHandleResult.Success; CommandReadResult rres = USB.ReadCommandInput(out NewCommandId cmdid); if (rres == CommandReadResult.Success) { // Console.WriteLine("Received command: " + cmdid.ToString()); switch (cmdid) { case NewCommandId.ListSystemDrives: { var drives = DriveInfo.GetDrives(); List <DriveInfo> adrives = new List <DriveInfo>(); uint count = (uint)drives.Length; if (drives.Length > 0) { foreach (var drive in drives) { if (drive.IsReady) { adrives.Add(drive); } } } USB.Write32((uint)adrives.Count); if (adrives.Count > 0) { foreach (var drive in adrives) { string label = drive.VolumeLabel; string prefix = drive.Name.Substring(0, 1); USB.WriteString(label); USB.WriteString(prefix); } } break; } case NewCommandId.GetPathType: { string spath = USB.ReadString(); string path = spath.NormalizeAsPath(); uint ptype = 0; if (File.Exists(path)) { ptype = 1; } else if (Directory.Exists(path)) { ptype = 2; } USB.Write32(ptype); break; } case NewCommandId.ListDirectories: { string spath = USB.ReadString(); string path = spath.NormalizeAsPath(); var ents = Directory.GetDirectories(path); USB.Write32((uint)ents.Length); if (ents.Length > 0) { foreach (var ent in ents) { string name = Path.GetFileName(ent); USB.WriteString(name); } } break; } case NewCommandId.ListFiles: { string spath = USB.ReadString(); string path = spath.NormalizeAsPath(); var ents = Directory.GetFiles(path); USB.Write32((uint)ents.Length); if (ents.Length > 0) { foreach (var ent in ents) { string name = Path.GetFileName(ent); USB.WriteString(name); } } break; } case NewCommandId.GetFileSize: { string spath = USB.ReadString(); string path = spath.NormalizeAsPath(); ulong size = 0; try { size = (ulong)new FileInfo(path).Length; } catch { size = 0; } USB.Write64(size); break; } case NewCommandId.FileRead: { ulong offset = USB.Read64(); ulong size = USB.Read64(); string spath = USB.ReadString(); string path = spath.NormalizeAsPath(); ulong rbytes = 0; Console.WriteLine("FileRead - Path: " + path + ", Offset: " + offset + ", Size: " + size); byte[] data = new byte[size]; try { var fs = new FileStream(path, FileMode.Open, FileAccess.Read) { Position = (long)offset }; rbytes = (uint)fs.Read(data, 0, (int)size); fs.Close(); } catch (Exception ex) { Console.WriteLine("FileRead (" + path + ") - Error reading file: " + ex.Message); } USB.Write64(rbytes); Console.WriteLine("FileRead - Read bytes: " + rbytes); if (rbytes > 0) { USB.WriteBytes(data); } break; } case NewCommandId.FileWrite: { string spath = USB.ReadString(); USB.Read32(); uint offset = USB.Read32(); uint size = USB.Read32(); byte[] data = USB.ReadBytes((int)size); string path = spath.NormalizeAsPath(); Console.WriteLine("FileWrite - Path: (" + path + "), Offset: " + offset + ", Size: " + size); try { var fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read); var bw = new BinaryWriter(fs); bw.BaseStream.Position = offset; bw.Write(data, 0, (int)size); bw.Close(); } catch { Console.WriteLine("FileWrite (" + path + ") - Error writing file"); } break; } case NewCommandId.CreateFile: { string spath = USB.ReadString(); string path = spath.NormalizeAsPath(); File.Create(path); break; } case NewCommandId.CreateDirectory: { string spath = USB.ReadString(); string path = spath.NormalizeAsPath(); Directory.CreateDirectory(path); break; } case NewCommandId.DeleteFile: { string spath = USB.ReadString(); string path = spath.NormalizeAsPath(); File.Delete(path); break; } case NewCommandId.DeleteDirectory: { string spath = USB.ReadString(); string path = spath.NormalizeAsPath(); Directory.Delete(path, true); break; } case NewCommandId.RenameFile: { string spath = USB.ReadString(); string newname = USB.ReadString(); string path = spath.NormalizeAsPath(); string npath = Path.Combine(Path.GetDirectoryName(path), newname); File.Move(path, npath); break; } case NewCommandId.RenameDirectory: { string spath = USB.ReadString(); string newname = USB.ReadString(); string path = spath.NormalizeAsPath(); string npath = Path.Combine(Path.GetDirectoryName(path), newname); Directory.Move(path, npath); break; } case NewCommandId.GetDriveTotalSpace: { string sdrive = USB.ReadString(); var drives = DriveInfo.GetDrives(); ulong space = 0; if (drives.Length > 0) { foreach (var drive in drives) { if (drive.Name.StartsWith(sdrive)) { space = (ulong)drive.TotalSize; break; } } } USB.Write64(space); break; } case NewCommandId.GetDriveFreeSpace: { string sdrive = USB.ReadString(); var drives = DriveInfo.GetDrives(); ulong space = 0; if (drives.Length > 0) { foreach (var drive in drives) { if (drive.IsReady && drive.Name.StartsWith(sdrive)) { space = (ulong)drive.TotalFreeSpace; break; } } } USB.Write64(space); break; } } } else { res = CommandHandleResult.BadInArguments; } return(res); }