Exemplo n.º 1
0
        public void ShowFilePicker(Picker picker)
        {
            try
            {
                SelectedFile = null;// new SmbInfo();

                var fileList = new List <SmbInfo>();

                if (TransferType == 1 && !string.IsNullOrEmpty(MySmbInfo.LocalDirectory))
                {
                    fileList = StorageService.GetFileList(MySmbInfo.LocalDirectory);
                }
                else if (TransferType == 0 && !string.IsNullOrEmpty(MySmbInfo.ServerDirectory))
                {
                    fileList = smbService.Smb2GetFileList(MySmbInfo);
                }

                FileList = new ObservableCollection <SmbInfo>(fileList);
                RaisePropertyChanged("FileList");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            picker.Focus();
        }
Exemplo n.º 2
0
 public void SelectedFileChanged(SmbInfo smb)
 {
     if (smb != null)
     {
         MySmbInfo.FileName = smb.FileName;
         if (TransferType == 1)
         {
             MySmbInfo.LocalDirectory = smb.LocalDirectory;
         }
     }
 }
Exemplo n.º 3
0
        public MainPageViewModel(INavigationService navigationService, IStorageService storageService, IPopupService popupService)
            : base(navigationService, storageService, popupService)
        {
            Title = "File Transfer via SMB2.0";

            //set default value
            MySmbInfo = new SmbInfo()
            {
                ServerDirectory = @"192.168.11.101/share",
                User            = "******",
                Password        = "******",
            };

            smbService = new SmbService();

            FileTransferCommand = new DelegateCommand(async() => await SmbFileTransfer());

            ImgRadioChecked   = ImageSource.FromResource("SMB2forXamarinForms.Images.round_radio_button_checked_black.png");
            ImgRadioUnchecked = ImageSource.FromResource("SMB2forXamarinForms.Images.round_radio_button_unchecked_black.png");

            ImgRadio = new List <ImageSource>();

            for (var i = 0; i < 2; i++)
            {
                ImgRadio.Add(ImgRadioUnchecked);
            }
            SelectRadio(0);
            //RaisePropertyChanged("ImgRadio");

            SelectRadioCommand = new DelegateCommand <string>(x => SelectRadio(Convert.ToInt32(x)));

            LocalDirectoryList = new List <string>()
            {
                "Downloads", "Dcim", "Pictures", "Movies", "Documents", "Music",
            };
            SelectedLocalDirectory = LocalDirectoryList[0];

            FilePickerShowCommand      = new DelegateCommand <Picker>((Picker picker) => ShowFilePicker(picker));
            DirectoryPickerShowCommand = new DelegateCommand <Picker>((Picker picker) => picker.Focus());
        }
Exemplo n.º 4
0
        public async Task <int> Smb2Upload(SmbInfo smb)
        {
            var res            = 0;
            var filePathServer = Path.Combine(smb.ServerDirectory, smb.FileName);
            var filePathLocal  = Path.Combine(smb.LocalDirectory, smb.FileName);

            try
            {
                var client = new SMBLibrary.Client.SMB2Client();

                var pathArr = filePathServer.Split(new char[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);

                if (pathArr.Length > 2 && client.Connect(System.Net.IPAddress.Parse(pathArr[0]), SMBTransportType.DirectTCPTransport))
                {
                    var status = client.Login(String.Empty, smb.User ?? "", smb.Password ?? "");
                    if (status == NTStatus.STATUS_SUCCESS)
                    {
                        var share = client.TreeConnect(pathArr[1], out status);
                        if (status == NTStatus.STATUS_SUCCESS)
                        {
                            var        path = string.Join(@"\", pathArr, 2, pathArr.Length - 2);
                            object     handle;
                            FileStatus fileStatus;
                            status = share.CreateFile(out handle, out fileStatus, path, AccessMask.GENERIC_READ | AccessMask.GENERIC_WRITE, 0, ShareAccess.None, CreateDisposition.FILE_OPEN_IF, CreateOptions.FILE_NON_DIRECTORY_FILE, null);
                            if (status == NTStatus.STATUS_SUCCESS)
                            {
                                using (var fs = new FileStream(filePathLocal, FileMode.Open, FileAccess.Read))
                                {
                                    var offset     = 0;
                                    var bufferSize = (int)client.MaxReadSize;

                                    do
                                    {
                                        var cnt    = (int)Math.Min(bufferSize, fs.Length - offset);
                                        var buffer = new byte[cnt];

                                        if (cnt <= 0)
                                        {
                                            break;
                                        }

                                        var readSize = await fs.ReadAsync(buffer, 0, cnt).ConfigureAwait(false);

                                        int numberOfBytesWritten;
                                        status = share.WriteFile(out numberOfBytesWritten, handle, offset, buffer);

                                        if (status != NTStatus.STATUS_SUCCESS)
                                        {
                                            throw new Exception(status.ToString());
                                        }

                                        offset += buffer.Length;
                                    }while (offset < fs.Length);
                                }

                                share.CloseFile(handle);
                                res = 1;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return(res);
        }
Exemplo n.º 5
0
        public List <SmbInfo> Smb2GetFileList(SmbInfo smb)
        {
            var fileList = new List <SmbInfo>();

            try
            {
                var client  = new SMBLibrary.Client.SMB2Client();
                var pathArr = smb.ServerDirectory.Split(new char[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);

                if (pathArr.Length > 1 && client.Connect(System.Net.IPAddress.Parse(pathArr[0]), SMBTransportType.DirectTCPTransport))
                {
                    var status = client.Login(String.Empty, smb.User ?? "", smb.Password ?? "");
                    if (status == NTStatus.STATUS_SUCCESS)
                    {
                        object handle;
                        SMBLibrary.FileStatus fileStatus;

                        var share = client.TreeConnect(pathArr[1], out status) as SMB2FileStore;
                        if (status == NTStatus.STATUS_SUCCESS)
                        {
                            var subDir = pathArr.Length > 2 ? string.Join(@"\", pathArr, 2, pathArr.Length - 2) : "";
                            status = share.CreateFile(out handle, out fileStatus, subDir, AccessMask.GENERIC_READ, 0, ShareAccess.Read, CreateDisposition.FILE_OPEN, CreateOptions.FILE_DIRECTORY_FILE, null);
                            if (status == NTStatus.STATUS_SUCCESS)
                            {
                                status = share.QueryDirectory(out List <QueryDirectoryFileInformation> files, handle, "*", FileInformationClass.FileDirectoryInformation);
                                foreach (var file in files)
                                {
                                    var fileinfo = (FileDirectoryInformation)file;
                                    if (fileinfo.FileAttributes != SMBLibrary.FileAttributes.Directory)
                                    {
                                        fileList.Add(new SmbInfo {
                                            ServerDirectory = smb.ServerDirectory,
                                            //LocalDirectory = smb.LocalDirectory,
                                            FileName = fileinfo.FileName,
                                        });
                                    }
                                }
                            }
                            else
                            {
                                Console.WriteLine("CreateFile Error:" + status.ToString());
                            }
                        }
                        else
                        {
                            Console.WriteLine("Tree Connect Error:" + status.ToString());
                        }
                    }
                    else
                    {
                        Console.WriteLine("Login Error:" + status.ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return(fileList);
        }
Exemplo n.º 6
0
        public async Task <int> Smb2Download(SmbInfo smb)
        {
            var res            = 0;
            var filePathServer = Path.Combine(smb.ServerDirectory, smb.FileName);
            var filePathLocal  = Path.Combine(smb.LocalDirectory, smb.FileName);

            try
            {
                var client = new SMBLibrary.Client.SMB2Client();

                var pathArr = filePathServer.Split(new char[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);

                if (pathArr.Length > 2 && client.Connect(System.Net.IPAddress.Parse(pathArr[0]), SMBTransportType.DirectTCPTransport))
                {
                    var status = client.Login(String.Empty, smb.User ?? "", smb.Password ?? "");
                    if (status == NTStatus.STATUS_SUCCESS)
                    {
                        var share = client.TreeConnect(pathArr[1], out status);
                        if (status == NTStatus.STATUS_SUCCESS)
                        {
                            var        path = string.Join(@"\", pathArr, 2, pathArr.Length - 2);
                            object     handle;
                            FileStatus fileStatus;

                            status = share.CreateFile(out handle, out fileStatus, path, AccessMask.GENERIC_READ, 0, ShareAccess.Read, CreateDisposition.FILE_OPEN, CreateOptions.FILE_NON_DIRECTORY_FILE, null);
                            if (status == NTStatus.STATUS_SUCCESS)
                            {
                                FileInformation fileInfo;
                                status = share.GetFileInformation(out fileInfo, handle, FileInformationClass.FileStandardInformation);
                                if (status == NTStatus.STATUS_SUCCESS)
                                {
                                    var len    = ((FileStandardInformation)fileInfo).EndOfFile;
                                    var offset = 0;

                                    using (var fs = new FileStream(filePathLocal, FileMode.OpenOrCreate, FileAccess.Write))
                                    {
                                        var    bufferSize = (int)client.MaxReadSize;
                                        byte[] buffer;

                                        do
                                        {
                                            int cnt = (int)Math.Min(bufferSize, len - offset);
                                            status = share.ReadFile(out buffer, handle, offset, cnt);
                                            if (status == NTStatus.STATUS_END_OF_FILE || buffer?.Length == 0)
                                            {
                                                break;
                                            }
                                            else if (status != NTStatus.STATUS_SUCCESS)
                                            {
                                                throw new Exception(status.ToString());
                                            }
                                            await fs.WriteAsync(buffer, 0, buffer.Length);

                                            offset += buffer.Length;
                                        }while (offset < len);
                                    }
                                    res = 1;
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return(res);
        }