/// <summary> /// Zip压缩至byte[] /// </summary> /// <param name="srcFolder"></param> /// <param name="最顶层是否是文件夹(如果不是,则是文件夹下的一个个文件)"></param> /// <returns></returns> public static byte[] CompressFromFolder(string srcFolder, bool topFolder) { AbstractFile memoryFile = new MemoryFile(); //AbstractFile memoryFile = new DiskFile(System.IO.Path.GetTempFileName()); ZipArchive archive = CreateZipArchive(memoryFile); if (topFolder) { AbstractFolder source = new DiskFolder(srcFolder); source.CopyTo(archive, true); } else { foreach (var i in archive.GetItems(false)) { i.CopyTo(archive, true); } } byte[] buffer; using (Stream destStream = memoryFile.OpenRead(FileShare.Read)) { buffer = new byte[destStream.Length]; destStream.Read(buffer, 0, buffer.Length); destStream.Close(); } memoryFile.Delete(); return(buffer); }
//发送本地磁盘 public static void SendDiskHandle() { folderlist = new List <DiskFolder>(); //获取本地磁盘 foreach (var item in System.IO.DriveInfo.GetDrives()) { //|| System.IO.DriveType.Removable.Equals(item.DriveType) //获取U盘名字有问题 if (System.IO.DriveType.Fixed.Equals(item.DriveType) || System.IO.DriveType.Removable.Equals(item.DriveType)) { DiskFolder disk = new DiskFolder { Name = item.Name, Lable = item.VolumeLabel, FullName = null, FileTyp = "disk" }; folderlist.Add(disk); } } //集合序列化为字符串 message = JsonConvert.SerializeObject(folderlist); //发送的字符串 sendMessage = string.Format("XiYou#{0}", message); //发送 SendDiskSocket(App.serverSocketCmd); //threadSendDisk = new Thread(new ParameterizedThreadStart(Send)); //if (serverSocketDisk != null) // threadSendDisk.Start(App.serverSocketDisk); }
private void Send(int i) { DiskFolder folder = null; if (App.PPTOperation == "PPTSHOW") { folder = new DiskFolder { Name = null, Lable = "PPTSHOW", FullName = diskbtnModel[i].Name }; } else if (App.PPTOperation == "PPTDO") { folder = new DiskFolder { Name = null, Lable = "PPTDO", FullName = diskbtnModel[i].Name }; } string message = JsonConvert.SerializeObject(folder); Command command = new Command { Flag = "2", Msg = message }; string sendmessage = JsonConvert.SerializeObject(command); App.SendData(sendmessage, App.socketCmd); }
//发送磁盘文件夹 public static void SendPathHandle(string path, string flag) { folderlist = new List <DiskFolder>(); DirectoryInfo info = new DirectoryInfo(path); //非磁盘, 非文件夹,非桌面 //即文件 if (info.Attributes != System.IO.FileAttributes.Directory && info.Name.Length != 3 && info.Name != "Desktop") { Process.Start(path); } else { FileSystemInfo[] files = info.GetFileSystemInfos(); foreach (var file in files) { if (file.Attributes == FileAttributes.Directory && file.Attributes != FileAttributes.Hidden) { DiskFolder folder = new DiskFolder { FullName = file.FullName, Name = file.Name, Lable = flag, FileTyp = file.Attributes.ToString() }; folderlist.Add(folder); } } if (flag == "PPTSHOW") { foreach (var file in files) { if (file.Attributes == FileAttributes.Archive && file.Attributes != FileAttributes.Hidden) { DiskFolder folder = new DiskFolder { FullName = file.FullName, Name = file.Name, Lable = flag, FileTyp = file.Attributes.ToString() }; folderlist.Add(folder); } } } else if (flag == "PPTDO") { foreach (var file in files) { if (file.Attributes == FileAttributes.Archive && file.Attributes != FileAttributes.Hidden && (file.Extension == ".pptx" || file.Extension == ".ppt")) { DiskFolder folder = new DiskFolder { FullName = file.FullName, Name = file.Name, Lable = flag, FileTyp = file.Attributes.ToString() }; folderlist.Add(folder); } } } message = JsonConvert.SerializeObject(folderlist); sendMessage = string.Format("XiYou#{0}", message); //发送 SendDiskSocket(App.serverSocketCmd); } }
/// <summary> /// Zip压缩文件夹至压缩文件 /// </summary> /// <param name="srcFolder"></param> /// <param name="destFile"></param> /// <returns></returns> public static string ZipFromFolder(string srcFolder, string destFile) { ZipArchive archive = CreateZipArchive(destFile, true); AbstractFolder source = new DiskFolder(srcFolder); source.CopyTo(archive, true); return(destFile); }
/// <summary> /// 解压缩压缩文件至文件夹 /// </summary> /// <param name="srcFile"></param> /// <param name="destFolder"></param> public static void UnzipToFolder(string srcFile, string destFolder) { ZipArchive archive = CreateZipArchive(srcFile, false); // Just as zipping means "copy to a zip archive", unzipping means // "copy from a zip archive". Let's copy all DLL files in a temp folder. AbstractFolder dest = new DiskFolder(destFolder); // filter = "*.*" will filter file without extention archive.CopyFilesTo(dest, true, true, null); }
private static void RestoreFolder(Configuration config, DiskFolder folder, string restoreFolder) { var saveFolder = folder.Path != "" ? Path.Combine(restoreFolder, folder.Path) : restoreFolder; saveFolder.EnsureExists(); foreach (var f in folder.Files) { var hash = f.Hash; if (!string.IsNullOrEmpty(f.Hash)) { var origin = @$ "{config.BackupFolder}\Files\{hash.Substring(0, 2)}\{hash.Substring(0, 5)}\{hash}";
public async void mycom_Click(object sender, RoutedEventArgs e) { String str = sender.ToString(); if (str.Equals("PPTDO")) { App.PPTOperation = "PPTDO"; } else { App.PPTOperation = "PPTSHOW"; } if (App.IsConnect == false) { await new MessageDialog("请先连接").ShowAsync(); } else { loadanimation.Begin(); DiskFolder folder = null; if (App.PPTOperation == "PPTDO") { folder = new DiskFolder { Name = "我的电脑", Lable = "我的电脑", FullName = "我的电脑" }; } else if (App.PPTOperation == "PPTSHOW") { folder = new DiskFolder { Name = "我的电脑", Lable = "我的电脑", FullName = "我的电脑" }; } //类的序列化 string message = JsonConvert.SerializeObject(folder); Command command = new Command { Flag = "2", Msg = message }; string sendmessage = JsonConvert.SerializeObject(command); App.SendData(sendmessage, App.socketCmd); if (App.flag == 0) { //tDisk = new Task((Action)(() => { App.ReceiveCmd(App.socketCmd); App.flag = 1; //})); //tDisk.Start(); loadanimation.Stop(); image1.Visibility = Visibility.Collapsed; image2.Visibility = Visibility.Collapsed; image3.Visibility = Visibility.Collapsed; } } }
private static void DiskHandle(DiskFolder folder) { if (folder != null) { if (folder.Name == "我的电脑" && folder.Lable == "我的电脑" && folder.FullName == "我的电脑") { SendDiskHandle(); } else { string path = ""; if (folder.FullName == "Desktop") { path = "C:\\Users\\" + Environment.UserName + "\\Desktop"; } else { path = folder.FullName; } ////显示文件 //if (folder.Operation == "Show") //{ if (folder.Lable == "PPTSHOW") { SendPathHandle(path, "PPTSHOW"); } else if (folder.Lable == "PPTDO") { SendPathHandle(path, "PPTDO"); } //} ////文件重命名 //else if (folder.Operation == "ReName") //{ // File.Move(path, "newfolder.txt"); //} ////移动文件 //else if (folder.Operation == "Move") //{ // File.Move(path, "D://newfolder.txt"); //} ////文件删除 //else if (folder.Operation == "Delete") //{ // File.Delete(path); //} } } }
private void disklistview_SelectionChanged(object sender, SelectionChangedEventArgs e) { disklistview.IsEnabled = false; DiskItemModel diskitem = disklistview.SelectedItem as DiskItemModel; string fullname = pathname.Text + diskitem.Name; DiskFolder folder = new DiskFolder { Name = null, Lable = "PPTSHOW", FullName = fullname }; string message = JsonConvert.SerializeObject(folder); Command command = new Command { Flag = "2", Msg = message }; string sendmessage = JsonConvert.SerializeObject(command); App.SendData(sendmessage, App.socketCmd); disklistview.IsEnabled = true; }
/// <summary> /// Zip解压缩byte[]至文件夹 /// </summary> /// <param name="srcByte"></param> /// <param name="destFolder"></param> public static void DecompressToFolder(byte[] srcByte, string destFolder) { AbstractFile memoryFile = new MemoryFile(); //AbstractFile memoryFile = new DiskFile(System.IO.Path.GetTempPath() + "memoryFile.zip"); memoryFile.Create(); using (Stream stream = memoryFile.OpenWrite(true, FileShare.Write)) { stream.Write(srcByte, 0, srcByte.Length); stream.Close(); } // 必须先写MemoryFile再创建ZipArchive ZipArchive archive = CreateZipArchive(memoryFile); AbstractFolder dest = new DiskFolder(destFolder); archive.CopyFilesTo(dest, true, true, null); memoryFile.Delete(); }
private void disklistview_SelectionChanged(object sender, SelectionChangedEventArgs e) { disklistview.IsEnabled = false; DiskItemModel diskitem = disklistview.SelectedItem as DiskItemModel; string fullname = pathname.Text + diskitem.Name; DiskFolder folder = new DiskFolder { Name = null, Lable = "PPTDO", FullName = fullname }; string message = JsonConvert.SerializeObject(folder); Command command = new Command { Flag = "2", Msg = message }; string sendmessage = JsonConvert.SerializeObject(command); App.SendData(sendmessage, App.socketCmd); if (diskitem.FileTyp == "Archive") { Frame rootFrame = (Window.Current.Content) as Frame; rootFrame.Navigate(typeof(PPTView)); } disklistview.IsEnabled = true; }
/// <summary> /// Zip解压缩byte[]至文件夹 /// </summary> /// <param name="srcByte"></param> /// <param name="destFolder"></param> public static void DecompressToFolder(byte[] srcByte, string destFolder) { AbstractFile memoryFile = new MemoryFile(); //AbstractFile memoryFile = new DiskFile(System.IO.Path.GetTempPath() + "memoryFile.zip"); memoryFile.Create(); using (Stream stream = memoryFile.OpenWrite(true, FileShare.Write)) { stream.Write(srcByte, 0, srcByte.Length); stream.Close(); } // 必须先写MemoryFile再创建ZipArchive ZipArchive archive = CreateZipArchive(memoryFile); AbstractFolder dest = new DiskFolder(destFolder); archive.CopyFilesTo(dest, true, true, null); memoryFile.Delete(); }
//接收socketDisk通道的信息 public async static void ReceiveCmd(object obj) { StreamSocket s = obj as StreamSocket; byte[] temp = null; byte[] buffer = null; byte[] bufferlength = null; byte[] recvbuffer = null; int length; while (true) { recvbuffer = new byte[0]; string info = null; DataReader reader = new DataReader(s.InputStream); reader.InputStreamOptions = InputStreamOptions.Partial; reader.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8; try { bufferlength = new byte[sizeof(Int32)]; await reader.LoadAsync(sizeof(Int32)); reader.ReadBytes(bufferlength); //reader.DetachStream(); length = BitConverter.ToInt32(bufferlength, 0); int notRcvLength = length; //作为接收缓存区,每次最多接收length的字节,最少大于0 buffer = new byte[length]; while (recvbuffer.Length < length || notRcvLength != 0) { uint count = await reader.LoadAsync((uint)length); notRcvLength -= (int)count; temp = new byte[count]; reader.ReadBytes(temp); recvbuffer = recvbuffer.Concat(temp).ToArray(); } reader.DetachStream(); //reader.Dispose(); } catch (Exception e) { Debug.WriteLine(e.Message); //await new MessageDialog(e.Message).ShowAsync(); return; } info = Encoding.UTF8.GetString(recvbuffer); Debug.WriteLine("SocketCmd接收的信息:" + info); //接收到的是磁盘信息 if (info != "" && info.Contains("XiYou#")) { string[] sendinfo = Regex.Split(info, "#"); //MainViewModel.m.rcvMsg = sendinfo[1]; //字符串反序列化 folderlist = JsonConvert.DeserializeObject <List <DiskFolder> >(sendinfo[1]); if (folderlist.Count > 0) { if (folderlist[0].Lable == "本地磁盘") { Frame frame = (Window.Current.Content) as Frame; EntranceNavigationTransitionInfo s1 = new EntranceNavigationTransitionInfo(); frame.Navigate(typeof(ComOperation), folderlist, s1); } else if (folderlist[0].Lable == "PPTSHOW") { Frame frame = (Window.Current.Content) as Frame; SuppressNavigationTransitionInfo s1 = new SuppressNavigationTransitionInfo(); frame.Navigate(typeof(DiskShow), folderlist, s1); } else if (folderlist[0].Lable == "PPTDO") { Frame frame = (Window.Current.Content) as Frame; SuppressNavigationTransitionInfo s1 = new SuppressNavigationTransitionInfo(); frame.Navigate(typeof(DiskPPTShow), folderlist, s1); } } else { if (App.PPTOperation == "PPTSHOW") { DiskFolder diskfolder = new DiskFolder(); diskfolder.FullName = ""; diskfolder.Name = "文件夹为空"; diskfolder.Lable = ""; diskfolder.FileTyp = ""; folderlist.Add(diskfolder); Frame frame = (Window.Current.Content) as Frame; SuppressNavigationTransitionInfo s1 = new SuppressNavigationTransitionInfo(); frame.Navigate(typeof(DiskShow), folderlist, s1); } else if (App.PPTOperation == "PPTDO") { DiskFolder diskfolder = new DiskFolder(); diskfolder.FullName = ""; diskfolder.Name = "文件夹为空"; diskfolder.Lable = ""; diskfolder.FileTyp = ""; folderlist.Add(diskfolder); Frame frame = (Window.Current.Content) as Frame; SuppressNavigationTransitionInfo s1 = new SuppressNavigationTransitionInfo(); frame.Navigate(typeof(DiskPPTShow), folderlist, s1); } } } else { byte[] writebuffer = new byte[length]; App.screenbuffer = GetSendBuffer(writebuffer, recvbuffer); } } }
public void UploadOrders() { try { Xceed.Ftp.Licenser.LicenseKey = "FTN42-K40Z3-DXCGS-PYGA"; //Xceed.FileSystem.Licenser.LicenseKey = ""; if (!UploadWork) { try { UploadWork = true; SqlConnection db_connection = new SqlConnection(prop.Connection_string); db_connection.Open(); file.WriteLine(DateTime.Now.ToString("g", ci) + " [+] Выбираем заказы на экспорт"); file.Flush(); SqlCommand db_command = new SqlCommand("SELECT [number], [auto_export], [id_place] FROM [order] WHERE [auto_export] > 0;", db_connection); SqlDataAdapter db_adapter = new SqlDataAdapter(db_command); DataTable tbl = new DataTable(); db_adapter.Fill(tbl); foreach (DataRow rw in tbl.Rows) { try { file.WriteLine(DateTime.Now.ToString("g", ci) + " [+] Подготавливаем к выгрузке заказ " + rw["number"].ToString().Trim()); file.Flush(); PSA.Lib.Util.ExportOrder.autoExport((int)rw["auto_export"], rw["number"].ToString().Trim()); db_command = new SqlCommand("SELECT [server], [path], [username], [password] FROM [place] WHERE [id_place] = " + rw["id_place"], db_connection); db_adapter = new SqlDataAdapter(db_command); DataTable ptbl = new DataTable(); db_adapter.Fill(ptbl); if (ptbl.Rows.Count > 0) { DataRow place = ptbl.Rows[0]; file.WriteLine(DateTime.Now.ToString("g", ci) + " [+] Выгружаем заказ " + rw["number"].ToString().Trim() + " на " + place["server"].ToString().Trim() + " в " + place["path"].ToString().Trim()); file.Flush(); using (FtpConnection connection = new FtpConnection( place["server"].ToString().Trim(), place["username"].ToString().Trim(), place["password"].ToString().Trim())) { connection.Encoding = Encoding.GetEncoding(1251); file.WriteLine("from: " + prop.Dir_export + "\\auto_export\\" + rw["number"].ToString().Trim() + "\\"); file.Flush(); DiskFolder source = new DiskFolder(prop.Dir_export + "\\auto_export\\" + rw["number"].ToString().Trim() + "\\"); string ftp_to = place["path"].ToString().Trim() + rw["number"].ToString().Trim() + "/"; if (ftp_to.Substring(0, 1) == "/") ftp_to = ftp_to.Substring(1); file.WriteLine("to: " + ftp_to); file.Flush(); try { FtpFolder _ftp_to = new FtpFolder(connection, ftp_to); _ftp_to.Delete(); } catch { } FtpFolder destination = new FtpFolder(connection, ftp_to); StreamWriter _tmp = new StreamWriter(prop.Dir_export + "\\auto_export\\" + rw["number"].ToString().Trim() + "\\.lock"); _tmp.Write("\n"); _tmp.Close(); db_command = new SqlCommand("UPDATE [order] SET [status_export] = 'Началась выгрузка', [status_export_date] = getdate() WHERE [number] = '" + rw["number"].ToString().Trim() + "'", db_connection); db_command.ExecuteNonQuery(); source.CopyFilesTo(destination, true, true); FtpFile _lock = new FtpFile(connection, ftp_to + ".lock"); _lock.Delete(); db_command = new SqlCommand("UPDATE [order] SET [auto_export] = -1, [status_export] = 'Отправлен', [status] = '500000', [status_export_date] = getdate() WHERE [number] = '" + rw["number"].ToString().Trim() + "'", db_connection); db_command.ExecuteNonQuery(); file.WriteLine(DateTime.Now.ToString("g", ci) + " [+] Выгружен заказ " + rw["number"].ToString().Trim()); file.Flush(); Directory.Delete(prop.Dir_export + "\\auto_export\\" + rw["number"].ToString().Trim() + "\\", true); } } } catch (Exception ex) { db_command = new SqlCommand("UPDATE [order] SET [status_export] = '" + ex.Message + "', [status_export_date] = getdate() WHERE [number] = '" + rw["number"].ToString().Trim() + "'", db_connection); db_command.ExecuteNonQuery(); file.WriteLine(DateTime.Now.ToString("g", ci) + " [!] Ошибка выгрузки заказа " + ex.Message + "\n" + ex.Source + "\n" + ex.StackTrace); file.Flush(); } } } catch (Exception ex) { file.WriteLine(DateTime.Now.ToString("g", ci) + " [!] Ошибка выгрузки заказов " + ex.Message + "\n" + ex.Source + "\n" + ex.StackTrace); file.Flush(); } finally { UploadWork = false; } } } catch (Exception ex) { file.WriteLine(DateTime.Now.ToString("g", ci) + " [!] Глобальная ошибка по время отправления " + ex.Message); file.Flush(); } }
/// <summary> /// Zip压缩文件夹至压缩文件 /// </summary> /// <param name="srcFolder"></param> /// <param name="destFile"></param> /// <returns></returns> public static string ZipFromFolder(string srcFolder, string destFile) { ZipArchive archive = CreateZipArchive(destFile, true); AbstractFolder source = new DiskFolder(srcFolder); source.CopyTo(archive, true); return destFile; }
/// <summary> /// Zip压缩至byte[] /// </summary> /// <param name="srcFolder"></param> /// <param name="最顶层是否是文件夹(如果不是,则是文件夹下的一个个文件)"></param> /// <returns></returns> public static byte[] CompressFromFolder(string srcFolder, bool topFolder) { AbstractFile memoryFile = new MemoryFile(); //AbstractFile memoryFile = new DiskFile(System.IO.Path.GetTempFileName()); ZipArchive archive = CreateZipArchive(memoryFile); if (topFolder) { AbstractFolder source = new DiskFolder(srcFolder); source.CopyTo(archive, true); } else { foreach (var i in archive.GetItems(false)) { i.CopyTo(archive, true); } } byte[] buffer; using (Stream destStream = memoryFile.OpenRead(FileShare.Read)) { buffer = new byte[destStream.Length]; destStream.Read(buffer, 0, buffer.Length); destStream.Close(); } memoryFile.Delete(); return buffer; }
private void button6_Click(object sender, EventArgs e) { try { Xceed.Ftp.Licenser.LicenseKey = "FTN42-K40Z3-DXCGS-PYGA"; using (FtpConnection connection = new FtpConnection( ftpAddress.Text, ftpUser.Text, ftpPassword.Text)) { connection.Encoding = Encoding.GetEncoding(1251); DiskFolder source = new DiskFolder(ftpSource.Text); FtpFolder destination = new FtpFolder(connection, ftpDestanation.Text); source.CopyFilesTo(destination, true, true); } MessageBox.Show("ok"); } catch (Exception ex) { MessageBox.Show(ex.Message + "\n" + ex.Source + "\n" + ex.StackTrace); } }
public static void ReceiveCmd(object obj) { Socket s = obj as Socket; byte[] buffer = null; while (true) { try { buffer = new byte[200]; s.Receive(buffer); } catch (ArgumentNullException) { //MessageBox.Show("发送的东西为空"); // s.Dispose(); //InfoHandle.threadDisk.Abort(); return; } catch (SocketException) { //MessageBox.Show("访问套接字失败"); return; } catch (ObjectDisposedException) { Debug.WriteLine("连接已断开"); //MessageBox.Show("连接已断开"); //DisConnect(); //Cmd.OnStartUp(); return; } string info = Encoding.UTF8.GetString(buffer); info = Msg.HandleRcvMsg(info); //考虑是否有# try { if (info.Contains("XiYou#")) { HandleRcvMsg(ref info); Debug.WriteLine("SocketCmd接收的信息:" + info); Command command = JsonConvert.DeserializeObject <Command>(info); if (command.Flag == "1") { HandleModel handle = JsonConvert.DeserializeObject <HandleModel>(command.Msg); Cmd.Command(handle); } else if (command.Flag == "2") { //json反序列为类 DiskFolder folder = JsonConvert.DeserializeObject <DiskFolder>(command.Msg); DiskHandle(folder); } else if (command.Flag == "3") { string[] offset = Regex.Split(command.Msg, ","); if (offset.Length >= 2 && offset.Length < 5) { var sgl1 = new Signal1(); sgl1.x = Convert.ToDouble(offset[0]); sgl1.y = Convert.ToDouble(offset[1]); sgl1.mode = Msg.HandleRcvMsg(offset[2]); Signal1.DoMouseEvent(sgl1); } } } } catch (Exception e) { Debug.WriteLine(e.Message); } } }
/// <summary> /// 解压缩压缩文件至文件夹 /// </summary> /// <param name="srcFile"></param> /// <param name="destFolder"></param> public static void UnzipToFolder(string srcFile, string destFolder) { ZipArchive archive = CreateZipArchive(srcFile, false); // Just as zipping means "copy to a zip archive", unzipping means // "copy from a zip archive". Let's copy all DLL files in a temp folder. AbstractFolder dest = new DiskFolder(destFolder); // filter = "*.*" will filter file without extention archive.CopyFilesTo(dest, true, true, null); }