Exemple #1
0
        private void NewKey(object sender, RoutedEventArgs e)
        {
            InputDialog id = new InputDialog("Новый код", "Введите новый код", InputDialog.Type.CODE, App.code.ToString());

            id.ShowDialog();
            if (id.GetResult() == null)
            {
                return;
            }
            App.code = int.Parse(id.GetResult());
            App.ini.WritePrivateString("Main", "code", App.code.ToString());
        }
        public void newFolder(object sender, RoutedEventArgs e)
        {
            InputDialog id = new InputDialog("Имя папки", "Введите имя папки", InputDialog.Type.FILENAME);

            id.ShowDialog();
            if (id.GetResult() == null)
            {
                return;
            }
            ShowDir sd = new ShowDir();

            sd.Type    = "newDir";
            sd.Name    = App.name;
            sd.Dir     = Directory;
            sd.DirName = id.GetResult();
            if (sc.mode != 0)
            {
                sd.Code = App.code % sc.mode;
            }
            writer.WriteLine(JsonConvert.SerializeObject(sd));
            writer.Flush();
        }
        public void Draw(string fileName, bool isFolder, String dir)
        {
            StackPanel sp = new StackPanel();

            sp.Margin      = new Thickness(10);
            sp.Orientation = Orientation.Horizontal;
            PackIcon pi = new PackIcon();

            pi.Foreground = new SolidColorBrush(Colors.White);
            pi.Kind       = isFolder ? PackIconKind.Folder : PackIconKind.File;
            pi.Height     = pi.Width = 20;
            TextBlock tb = new TextBlock();

            tb.Text       = fileName;
            tb.Foreground = new SolidColorBrush(Colors.White);
            tb.FontSize   = 15;
            tb.Padding    = new Thickness(10, 0, 0, 0);
            sp.Children.Add(pi);
            sp.Children.Add(tb);
            ListViewItem lvi = new ListViewItem();

            lvi.Content = sp;
            FilesList.Items.Add(lvi);

            ContextMenu menu = new ContextMenu();

            lvi.ContextMenu = menu;
            MenuItem delete = new MenuItem();

            delete.Header = "Удалить";
            menu.Items.Add(delete);
            delete.Click += delegate
            {
                Delete del = new Delete();
                del.Type     = "deleteFile";
                del.Name     = App.name;
                del.Dir      = dir;
                del.FileName = fileName;
                if (sc.mode != 0)
                {
                    del.Code = App.code % sc.mode;
                }
                writer.WriteLine(JsonConvert.SerializeObject(del));
                writer.Flush();
            };
            MenuItem rename = new MenuItem();

            rename.Header = "Переименовать";
            menu.Items.Add(rename);
            rename.Click += delegate
            {
                InputDialog id = new InputDialog("Имя файла", "Введите имя файла", InputDialog.Type.FILENAME, fileName);
                id.ShowDialog();
                if (id.GetResult() == null)
                {
                    return;
                }
                Rename rn = new Rename();
                rn.Type        = "renameFile";
                rn.Name        = App.name;
                rn.Dir         = Directory;
                rn.FileName    = fileName;
                rn.NewFileName = id.GetResult();

                if (sc.mode != 0)
                {
                    rn.Code = App.code % sc.mode;
                }
                writer.WriteLine(JsonConvert.SerializeObject(rn));
                writer.Flush();
            };

            if (!isFolder)
            {
                MenuItem Download = new MenuItem();
                Download.Header = "Загрузить";
                menu.Items.Add(Download);
                Download.Click += delegate
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    string         ext = System.IO.Path.GetExtension(fileName);
                    sfd.Filter = ext.Substring(1, ext.Length - 1).ToUpper() + " file|*" + ext + "|All files(*.*)|*.*";
                    if (sfd.ShowDialog() != true)
                    {
                        return;
                    }
                    new Thread(() =>
                    {
                        using (FileStream fstream = new FileStream(sfd.FileName, FileMode.Create))
                        {
                            Socket downloadSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                            downloadSocket.Bind(new IPEndPoint(IPAddress.Any, 0));
                            Download download       = new Download();
                            download.Type           = "downloadFile";
                            download.Name           = App.name;
                            download.Dir            = dir;
                            download.FileName       = fileName;
                            download.FileSocketPort = (downloadSocket.LocalEndPoint as IPEndPoint).Port;
                            if (sc.mode != 0)
                            {
                                download.Code = App.code % sc.mode;
                            }
                            writer.WriteLine(JsonConvert.SerializeObject(download));
                            writer.Flush();
                            downloadSocket.Listen(1);
                            Socket downloadSocketConnected = downloadSocket.Accept();
                            NetworkStream ns = new NetworkStream(downloadSocketConnected);
                            ns.CopyTo(fstream);
                            ns.Close();
                            downloadSocketConnected.Close();
                            downloadSocket.Close();
                            Application.Current.Dispatcher.Invoke(new Action(() =>
                            {
                                InfoDialog id = new InfoDialog("Загрузка завершена", "Загрузка файла " + fileName + " завершена");
                                id.Show();
                            }));
                        }
                    }).Start();
                };
            }

            lvi.PreviewMouseRightButtonDown += delegate(object sender, MouseButtonEventArgs e) { e.Handled = true; };
            lvi.Selected += delegate
            {
                if (isFolder)
                {
                    ShowDir sd = new ShowDir();
                    sd.Type    = "start";
                    sd.Name    = App.name;
                    sd.Dir     = dir;
                    sd.DirName = fileName;
                    if (sc.mode != 0)
                    {
                        sd.Code = App.code % sc.mode;
                    }
                    writer.WriteLine(JsonConvert.SerializeObject(sd));
                    writer.Flush();
                }
            };
        }