Ejemplo n.º 1
0
        private void GenerateButton_OnClick(object sender, RoutedEventArgs e)
        {
            var canGenerate = true;

            if (canGenerate)
            {
                canGenerate = Path.IsPathRooted(generatePathTextBox.Text);
            }

            if (canGenerate)
            {
                try
                {
                    using (File.Create(generatePathTextBox.Text)){ }
                    File.Delete(generatePathTextBox.Text);
                }
                catch
                {
                    canGenerate = false;
                }
            }

            if (canGenerate)
            {
                try
                {
                    if (int.Parse(lineLengthTextBox.Text) < 0 ||
                        long.Parse(fileSizeTextBox.Text) < 0)
                    {
                        canGenerate = false;
                    }
                    else
                    {
                        if (!seedTextBox.Text.Equals(""))
                        {
                            int.Parse(seedTextBox.Text);
                        }
                    }
                }
                catch
                {
                    canGenerate = false;
                }
            }

            if (canGenerate == false)
            {
                MessageBox.Show("Can't generate file: " + "\"" + generatePathTextBox.Text + "\""
                                , "Error"
                                , MessageBoxButton.OK
                                , MessageBoxImage.Error
                                , MessageBoxResult.OK
                                , MessageBoxOptions.None
                                );
            }
            else
            {
                var fileFullName = generatePathTextBox.Text;
                var lineLength   = int.Parse(lineLengthTextBox.Text);
                var fileSize     = long.Parse(fileSizeTextBox.Text);

                var seed = seedTextBox.Text.Equals("") ?
                           new Random().Next()
                    :
                           int.Parse(seedTextBox.Text);

                FileGenerator generator = new TextGenerator(seed, lineLength, fileSize);
                var           start     = DateTime.Now;
                generator.GenerateFile(fileFullName);
                showDone(DateTime.Now - start);
            }
        }