當前位置: 首頁>>代碼示例>>C#>>正文


C# OpenFileDialog.OpenFiles方法代碼示例

本文整理匯總了C#中Microsoft.Win32.OpenFileDialog.OpenFiles方法的典型用法代碼示例。如果您正苦於以下問題:C# OpenFileDialog.OpenFiles方法的具體用法?C# OpenFileDialog.OpenFiles怎麽用?C# OpenFileDialog.OpenFiles使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Microsoft.Win32.OpenFileDialog的用法示例。


在下文中一共展示了OpenFileDialog.OpenFiles方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: TryLoadImage

        public ImageViewModel TryLoadImage()
        {
            OpenFileDialog fileDialog = new OpenFileDialog()
                                        {
                                            CheckFileExists = true,
                                            CheckPathExists = true,
                                            Multiselect = true,
                                            Filter = "JPG Files (*.jpg)|*.jpg|JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png"
            };

            Func<string, bool> isValidFile = file =>
                                             {
                                                 string[] extensions =
                                                 {
                                                     ".jpg",
                                                     ".png",
                                                     ".jpeg"
                                                 };

                                                 string ext = Path.GetExtension(file);
                                                 return extensions.Contains(ext);
                                             };

            fileDialog.FileOk += (sender, args) =>
                                 {
                                     if (!fileDialog.FileNames.All(isValidFile))
                                         args.Cancel = true;
                                 };

            bool? dialogResult = fileDialog.ShowDialog();
            if (dialogResult.HasValue && dialogResult.Value)
            {
                try
                {
                    Stream[] files = fileDialog.OpenFiles();
                    for (int i = 0; i < fileDialog.FileNames.Length; i++)
                    {
                        string filePath = fileDialog.FileNames[i];
                        Stream file = files[i];
                        byte[] fileData = null;

                        using (var stream = new MemoryStream())
                        {
                            file.CopyTo(stream);
                            fileData = stream.ToArray();
                        }

                        ImageViewModel viewModel = new ImageViewModel(0, Path.GetFileName(filePath), fileData);

                        return viewModel;
                    }
                }
                catch (Exception ex)
                {
                }
            }

            return null;
        }
開發者ID:kobyb1988,項目名稱:PhotoBox,代碼行數:59,代碼來源:ImageLoadService.cs

示例2: AddItems

        /// <summary>
        /// Adds the items.
        /// </summary>
        /// <param name="files">The files.</param>
        private void AddItems(OpenFileDialog files)
        {
            Stream[] fileStreams = files.OpenFiles();
            for (int index = 0; index < files.FileNames.Length; index++)
            {
                Gallery gallery = ((Gallery)galleryListBox.SelectedItem);

                ClientImage image = new ClientImage
                                        {
                                            ImageName = TruncatePath(files.FileNames[index]),
                                            GalleryName = gallery.Name,
                                            GalleryId = gallery.GalleryID,
                                            PhotoBytes = GetByteArrayFromStream(fileStreams[index])

                                         };

                uploadListBox.Items.Add(image);
            }
        }
開發者ID:chuckconway,項目名稱:chucksoft-gallery,代碼行數:23,代碼來源:Main.xaml.cs

示例3: OpenFile_Click

        private void OpenFile_Click(object sender, RoutedEventArgs e)
        {
            filePath.Clear(); textBox_Files.Text = "";

            StringBuilder sb = new StringBuilder();
            OpenFileDialog dialog = new OpenFileDialog();

            dialog.Multiselect = true;
            dialog.Filter = "Text Files (.txt)|*.txt";
            dialog.ShowDialog();

            if (dialog.OpenFiles() != null)
            {
                filePath.AddRange(dialog.FileNames);
                sb.AppendLine("(" + filePath.Count + "шт.) ");

                for (int i = 0; i < filePath.Count; i++)
                {
                    sb.AppendLine(System.IO.Path.GetFileName(filePath[i]));
                }

                textBox_Files.Text = sb.ToString();
            }
        }
開發者ID:AlexKrainov,項目名稱:WiFi,代碼行數:24,代碼來源:MainWindow.xaml.cs

示例4: AddPatterns

        private void AddPatterns()
        {
            OpenFileDialog fileDialog = new OpenFileDialog()
            {
                CheckFileExists = true,
                CheckPathExists = true,
                Multiselect = true,
                Filter = "JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg"
            };

            Func<string, bool> isValidFile = file =>
            {
                string[] extensions =
                {
                    ".png",
                    ".jpeg",
                    ".jpg"
                };

                string ext = Path.GetExtension(file);
                return extensions.Contains(ext);
            };

            fileDialog.FileOk += (sender, args) =>
            {
                if (!fileDialog.FileNames.All(isValidFile))
                    args.Cancel = true;
            };

            bool? dialogResult = fileDialog.ShowDialog();
            if (dialogResult.HasValue && dialogResult.Value)
            {
                try
                {
                    Stream[] files = fileDialog.OpenFiles();
                    for (int i = 0; i < fileDialog.FileNames.Length; i++)
                    {
                        string filePath = fileDialog.FileNames[i];
                        Stream file = files[i];
                        byte[] fileData = null;

                        using (var stream = new MemoryStream())
                        {
                            file.CopyTo(stream);
                            fileData = stream.ToArray();
                        }

                        CheckablePatternDataViewModel viewModel = new PatternDataViewModel(Path.GetFileName(filePath), Pattern.PatternType, fileData)
                            .ToCheckable(ItemState.Added);

                        Children.Add(viewModel);
                    }
                }
                catch (Exception ex)
                {
                }

                CheckItem();
            }
        }
開發者ID:kobyb1988,項目名稱:PhotoBox,代碼行數:60,代碼來源:PatternManageViewModel.cs


注:本文中的Microsoft.Win32.OpenFileDialog.OpenFiles方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。