Problems with Folderbrowserdialog. Application hangs

Asked

Viewed 51 times

1

I have an application (WPF) that has been working for a couple of months without problems. I needed to make a change. When doing, in the line where you should choose a printer to store the result of my application, the application hangs (it seems to get lost). This started yesterday. I went home and today, in the first hour, I was able to make it work. After it worked, from there it didn’t work anymore, giving back the error. If I comment the line and fixed a printer, the application works(obvious). That’s the line that hangs: if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK).

Below the complete code of the method:

private void CriarZip()
    {
        string path_destino = string.Empty;
        string path_files = caminho_original + @"\Destino\Temp";   

        System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();

        fbd.Description = "Selecione a pasta para armazenar o arquivo zipado.";
        fbd.RootFolder = Environment.SpecialFolder.MyComputer;
        fbd.ShowNewFolderButton = true;

        if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            path_destino = fbd.SelectedPath;

        List<string> _filesDiretory = new List<string>();

        string nome_arquivo = nome_arquivo_zip + ".zip";

        if (!nome_arquivo.Contains(".zip"))
        {
            MessageBox.Show("O nome do arquivo deve possuir a extensão .zip");
            return;
        }

        try
        {
            string[] files_new = Directory.GetFiles(path_files, "*", SearchOption.AllDirectories);
            string[] folder_new = Directory.GetDirectories(path_files, "*", SearchOption.AllDirectories);

            CriaPastaFarmInterna();
            CriaPastaFarmExterna();

            //Deleto os arquivo que não estão na Farm Externa
            foreach (var file in files_new)
            {
                string t = string.Empty;
                int pos = file.IndexOf(dirInicio);

                if (pos > 0)
                {
                    t = file.ToString().Substring(pos, file.Length - pos);

                    bool bListaArquivo = (from b in listaArquivosForaFarmExterna
                                          where b.Contains(t)
                                          select b).Count() > 0 ? true : false;
                    if (bListaArquivo)
                        File.Delete(file);
                }
                else
                {
                    t = Path.GetFileName(file);
                    arquivos.Add(t);
                }  

            }

            LimpaPastaWeb();
            DeletaPastaFarmExterna();

            //Adiciono arquivos que estão dentro da pasta base apenas
            foreach (var file in Directory.GetFiles(path_files))                    
            {
                arquivos.Add(file);
            }

            //Aqui pego as pastas com arquivos que serão zipadas
            foreach (var file in Directory.GetDirectories(path_files))
            {
                arquivos.Add(file);
            }

            string localNomeDestinoZIP = path_destino + "\\" + nome_arquivo;

            if (arquivos.Count() > 0)
            {

                processaDiretorio(path_files);
                ZipUnzip.CriarArquivoZip(arquivos, localNomeDestinoZIP);
                MessageBox.Show("Os arquivos selecionados foram compactados na pasta \n\n " +
                          localNomeDestinoZIP);
            }
            else
                MessageBox.Show("Não há a pasta para ser compactada.");                

        }
        catch (Exception ex)
        {
            MessageBox.Show("Ocorreu um erro ao criar arquivo ZIP \n\n " + ex.Message);
            //zipgif.Visibility = System.Windows.Visibility.Hidden;
        }
        finally
        {
            DeletarPastaTrabalho(caminho_original);
            //zipgif.Visibility = System.Windows.Visibility.Hidden;
            Application.Current.Shutdown();
        }
    }

1 answer

0

try to add in the references:

using System.Windows.Forms;

and then instantiate in the following manner:

DialogResult result = new DialogResult();

if (result == DialogResult.OK)
    path_destino = fbd.SelectedPath;
  • This has already been tried and failed. I need the Folderbrowserdialog because what I need is to select a workbook.

  • I made an edition and put the original code. The previous code was the second tentative and was wrong.

  • and if you set to Environment.SpecialFolder.Desktop ? I believe it works

  • weird even, but modify this line, fbd.Rootfolder = Environment.SpecialFolder.Mycomputer; change Mycomputer to Desktop

  • I am beginning to distrust my environment, visual studio and so on. I will do the following. I’m going to fix the folder in the code, first make the changes I need, then I’ll rewrite the code and compile it and send it to type-approval and see if it gives the same problem. If it doesn’t happen, then I’ll be sure it’s my environment. If so, then I use the remaining time to understand and solve this problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.