private static void explorerDownload(string connectIP, string[] downloadInfo) { Task.Factory.StartNew(() => { try { string savePath = downloadInfo[1]; int serverPort = int.Parse(downloadInfo[2]); clientDownload = new TCP.Client(connectIP, serverPort); if (change_file) { byte[] fileData = null; FileInfo fi = new FileInfo(savePath); clientDownload.Send(fi.Length.ToString()); clientDownload.RecvString(); using (FileStream fs = new FileStream(savePath, FileMode.Open)) { fileData = new byte[fs.Length]; fs.Read(fileData, 0, (int)fs.Length); } clientDownload.Send(fileData); fileData = null; } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } if (clientDownload != null) { clientDownload.Close(); } clientDownload = null; }); }
public static void StartExplorerAccess(string connectIP, int connectPort, bool accessFile, bool changeFile, MainForm mainForm) { Task.Factory.StartNew(() => { try { clientExplorer = new TCP.Client(connectIP, connectPort); change_file = changeFile; main = mainForm; if (accessFile) { main.status_file.Visible = true; while (true) { string rawCmd = clientExplorer.RecvString(); string[] info = rawCmd.Split(' '); string cmd = info[0]; if (cmd == "+GET_DRIVES") { clientExplorer.Send(string.Join("|", Directory.GetLogicalDrives())); } else if (cmd == "+GET_DIR_LIST") { explorerGetDirList(rawCmd.Split('|')[1]); } else if (cmd == "+GET_MAIN_DRIVE") { clientExplorer.Send(Path.GetPathRoot(System.Environment.GetFolderPath(Environment.SpecialFolder.System))); } else if (cmd == "+UPLOAD") { explorerUpload(connectIP, rawCmd.Split('|')); } else if (cmd == "+DOWNLOAD") { explorerDownload(connectIP, rawCmd.Split('|')); } else if (cmd == "+COPY") { explorerCopy(rawCmd.Split('|')); } else if (cmd == "+DELETE") { explorerDelete(rawCmd.Split('|')[1]); } else if (cmd == "") { throw new Exception(); } GC.Collect(); } } else { throw new Exception(); } } catch { if (clientExplorer != null) { clientExplorer.Close(); } clientExplorer = null; main.status_file.Visible = false; } }); }