# MediaManagement [C#]

This example shows how to retrive groups and medias from specific manager, and how to upload a new media.

[C#]

       //Initialize the XAML object and retrieve group from server by login
       public MediaManagement(string login, string password)
       {
           InitializeComponent();

           //Login and retrive group
           List<View_Group> listResult;
           sample.GetGroup(out listResult, login, password);

           //Bind group to TreeList
           treeGroup.ItemsSource = TreeNode.GetTreeNode(listResult);
       }

[C#]

       //Select a group and retrive coresponding medias
       private void treeGroup_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
       {
           //Get Medias for selected group
           List<View_Media> listResult;
           sample.GetMedia(out listResult, (treeGroup.SelectedItem as TreeNode).Id ?? 0);

           //Update media thumbnail path
           listResult.ForEach(x => x.ThumbnailPath = (x.ThumbnailPath != null) ? (GlobalParam.MEDIA_URL + x.ThumbnailPath) : "");

           //Bind media to List
           gridMedia.ItemsSource = new ObservableCollection<View_Media>(listResult);
       }

[C#]

       //Upload media by packet
       private void btnUpload_Click(object sender, RoutedEventArgs e)
       {
           //Select a media
           OpenFileDialog openFileDialog = new OpenFileDialog();
           openFileDialog.Filter =
               "All Files (*.swf, *.swa, *.bmp, *.gif, *.jpg, *.jpeg, *.png, *.avi, *.qt, *.mpg, *.mpeg, *.m1v, *.mp2, *.wmv, *.qt, *.mov, *.mp4, *.m2v, *.3gp, *.3g2, *.divx)|*.swf; *.swa; *.bmp; *.gif; *.jpg; *.jpeg; *.png; *.avi; *.qt; *.mpg; *.mpeg; *.m1v; *.mp2; *.wmv; *.qt; *.mov; *.mp4; *.m2v; *.3gp; *.3g2; *.divx|" +
               "Flash Files (*.swf, *.swa)|*.swf; *.swa|" +
               "Image Files (*.bmp, *.gif, *.jpg, *.jpeg, *.png)|*.bmp; *.gif; *.jpg; *.jpeg; *.png|" +
               "Video files (*.avi, *.qt, *.mpg, *.mpeg, *.m1v, *.mp2, *.wmv, *.qt, *.mov, *.mp4, *.m2v, *.3gp, *.3g2, *.divx)|*.avi; *.qt; *.mpg; *.mpeg; *.m1v; *.mp2; *.wmv; *.qt; *.mov; *.mp4; *.m2v; *.3gp; *.3g2; *.divx";

           if(openFileDialog.ShowDialog() == true)
           {
               FileInfo fileInfo = new FileInfo(openFileDialog.FileName);
               Stream fileStream = fileInfo.OpenRead();

               //Create buffer for each packet of media
               byte[] buffer = new byte[Math.Min(fileInfo.Length, 4 * 1024 * 1024)];
               int bytesRead = 0;

               View_Media media = null;

               bool isFirst = true; bool isLast = false;
               while (!isLast)
               {
                   //Read media packet
                   bytesRead += fileStream.Read(buffer, 0, buffer.Length);
                   isLast = bytesRead >= fileInfo.Length;

                   //Send packet to server
                   sample.UploadMedia(out media, openFileDialog.SafeFileName, (treeGroup.SelectedItem as TreeNode).Id ?? 0, isFirst, isLast, buffer);

                   isFirst = false;
               }

               //Update media thumbnail path
               media.ThumbnailPath = (media.ThumbnailPath != null) ? (GlobalParam.MEDIA_URL + media.ThumbnailPath) : "";

               //Add media to collection
               (gridMedia.ItemsSource as ObservableCollection<View_Media>).Add(media);
           }
       }

[C#]

       //Delete media
       private void btnDelete_Click(object sender, RoutedEventArgs e)
       {
           sample.DeleteMedia(gridMedia.SelectedItem as View_Media,(treeGroup.SelectedItem as TreeNode).Id ?? 0);

           // for refresh the list
           treeGroup_SelectedItemChanged(null, null);
       }