C# Programming Random Number File Writer: Create an application that writes a se
ID: 3594652 • Letter: C
Question
C# Programming
Random Number File Writer:
Create an application that writes a series of random numbers to a file. Each random number should be in range of 1 through 100. the application shoukld let the user specify how many random number the file will hold and should use a SaveFileDialog control to let the user specify the file's name and location.
Random Number File Reader:
Create another application that uses an OpenFileDialog control to let theuser select the file that was created by the application/ This application should read the numbers from the file, display the numbers in a ListBox control. and then display the folling data:
-The total of numbers
-The number of random numbers read from the file
Explanation / Answer
using System;
using System.IO;
using System.Windows;
using Microsoft.Win32;
using System.Windows.Forms;
class ProgramDialog
{
private static void saveFile()
{
// this will read number from console
string input = Console.Readline();
Random random = new Random();
List<double> list = new List<double>();
int parsed;
// parse to int, needs error checking (will throw exception when input is not a valid int)
int.TryParse(input, out parsed);
for(int i = 0; i < parsed; i++)
{
list.Add(random.Next(100)); // generating random interger and adding to list
}
// this will open the save file dialog
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Text file (*.txt)|*.txt|C# file (*.cs)|*.cs"; // this is filtering the file extensions
if(saveFileDialog.ShowDialog() == true){
// this will write the number into file
TextWriter tw = new StreamWriter(saveFileDialog.FileName);
for(int i = 0; i < list.Count; i++)
tw.WriteLine(s); //writing the numbers to file
tw.Close();
}
}
private static void openFile(){
// Random Number File Reader:
// Show the Dialog.
// If the user clicked OK in the dialog and
// a .CUR file was selected, open it.
OpenFileDialog theDialog = new OpenFileDialog();
theDialog.Title = "Open Text File";
theDialog.Filter = "TXT files|*.txt";
theDialog.InitialDirectory = @"C:";
String filename;
String path;
if (theDialog.ShowDialog() == DialogResult.OK)
{
filename = System.IO.Path.GetFileName(ofd.FileName);
path = System.IO.Path.GetDirectoryName(ofd.FileName);
}
string[] lines = System.IO.File.ReadAllLines(path);
// Display the file contents by using a foreach loop.
foreach (string line in lines)
{
// Use a tab to indent each line of the file.
Console.WriteLine(" " + line);
}
}
static void Main()
{
ProgramDialog.saveFile();
ProgramDialog.openFile();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.