Hello my console app is for downloading html files and then sending it to a txt file, after the process is complete I would like the app to verify if the file was created and then close the program after telling the user that the page downloaded. I would like to ask how to verify if file exists and then how to exit the app.
Console.WriteLine("Enter a Valid Webpage URL such as http://wwww.google.com, this tool will download the HTML into a .txt file");
string webPage = Console.ReadLine();
Uri uriResult;
bool result = Uri.TryCreate(webPage, UriKind.Absolute, out uriResult)
&& (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
WebClient client = new WebClient();
//DownloadsWebpage
string reply = client.DownloadString(webPage);
Console.WriteLine(reply);
//Location of file here
Console.WriteLine("Please enter a valid address to save the .txt file such as C:\\Users\\Public\\String.txt, then press any button to exit");
string txtDir = Console.ReadLine();
File.WriteAllText(txtDir, reply);
Console.ReadLine();
For verifying a file exists, there is a method called File.Exists() which would tell you if it has been created. Then once your program gets to the end of main it will just edit automatically.
Something like the following after your File.WriteAllText(txtDir, reply); may be:
if (!File.Exists(txtDir)) {
Console.WriteLine("Some error creating file");
}
When you want to exit, just let you program close when it reaches the end of main or use Enviroment.Exit(0)
To check if the file Exists and exit:
if( System.IO.File.Exists(pathname))
{
System.Environment.Exit(0);
}
// Do something else here to recover
Related
I need to create a console application that can take multiple different launch arguments (which can be added using the batch file). I so far tried this, but it seems like I don't understand it correctly.
Code:
static void Main(string[] args)
{
ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.Arguments = LaunchArguments.Operation_AddLocale;
processStartInfo.Arguments = LaunchArguments.Operation_CreateTextFile;
processStartInfo.UseShellExecute = false;
if (processStartInfo.Arguments == LaunchArguments.Operation_CreateTextFile)
{
Console.WriteLine("Creating a text file.");
File.Create("file.txt");
Console.Write("Done!");
}
if (processStartInfo.Arguments == LaunchArguments.Operation_AddLocale)
{
if (Directory.Exists("locale"))
{
try
{
File.Create("locale.txt");
}
catch (Exception ex)
{
Console.WriteLine("An error occured: ");
Console.Write(ex.ToString());
Console.Write(ex.Message.ToString());
Console.ReadLine();
}
}
else
{
Console.WriteLine("Incorrect Input, quitting");
Console.WriteLine("This application only accepts arguments of type '-CreateTextFile; -AddLocale'");
Console.Beep();
Console.ReadLine();
}
}
I made a constant string 'Operation_AddLocale' and 'Operation_CreateTextFile'.
by batch file:
#echo off
start SW_project_generator.exe -AddLocale
This all should do that if I launch the application via this batch file, it will do the operations that are in the 'if (processStartInfo.Arguments == LaunchArguments.Operation_AddLocale)' and if the batch file would add the '-CreateTextFile' argument, it will go to the 'if (processStartInfo.Arguments == LaunchArguments.Operation_CreateTextFile)'.
However, when I launch this app via my batch file, it will always just use the first argument (which is the '-CreateTextFile') and creates a text file and then goes to the else option.
Application's Output:
Creating a text file.
Done!Incorrect Input, quitting
This application only accepts arguments of type '-CreateTextFile; -AddLocale;'
Alright so, my question is, how to make this working, that if I create a batch file give it a argument of type '-AddLocale' it will just go to the 'AddLocale' operations and if I give it something else that is defined in the app, like the 'CreateTextFile', it'll go to it's 'if' statement. And finnaly, if the launch argument will be equal to nothing or wrong one, it'll show the quitting message.
Thanks everyone for help.
I need to create an installer for my operating system in Cosmos kernel but I don't know where to start or how to create it.
[See my Operating system]
https://www.mediafire.com/file/3nr6pbjfg7p1gfq/GoPixelOS.iso/file
I want to install the OS on the machine, how do I do that?
I think I can help you. but it actually makes a new user. but it still needs installation. but this won't work for another computer that does not have Filesystem.vmdk. maybe it is just a virtual file system
First you will need a file system
you will also need System.io
on before writing this code⬇
if (!File.Exists("0:\\youros\\System.txt")) //you will need Using.System.io and ! mean not equal
{
Console.Clear();
Console.WriteLine("YourOS will be install in your pc");
Console.WriteLine("Create a username and password:");
Console.Write("Username: ");
string username = Console.ReadLine();
Console.Write("Password: ");
string cPassword = Console.ReadLine();
Console.Write(username +"> "); //when your put username variable it should look like this "The user name: "
Console.WriteLine("Creating System Directory..."); //Creating Directory or folder
fs.CreateDirectory("0:\\yourOS\\");
Console.WriteLine("Creating File for user");
fs.CreateFile("0:\\yourOS\\System.txt"); //Creating File System
fs.CreateFile("0:\\yourOS\\users.db");
fs.CreateFile("0:\\yourOS\\password.db"); //creating a password files
Console.WriteLine("Setting User Preferences...");
File.WriteAllText("0:\\yourOS\\System.txt", write);
File.WriteAllText("0:\\yourOS\\users.db", username); ` //This will save username
File.WriteAllText("0:\\yourOS\\password.db", cPassword); //this one will save the user password
Console.WriteLine("Reboot ONEOS in 3 seconds");
Cosmos.HAL.Global.PIT.Wait(3000);
Sys.Power.Reboot(); //when the installation done it will reboot`
else //after reboot it will go to else and just leave it blank. it will just go to run() after the else
{
password = File.ReadAllText("0:\\oneos\\password.db"); //this will show the
Console.Write("Type your password: ")
var type Console.ReadLine();
if (type == password)
{
//just leave it blank. it will go to protected override void run()
}
else
}
Sys.Power.Reboot(); //this will reboot the OS if user type wrong password
{
}
That is for the installation but if you want the installation to be more useful you will need this
//protected override void Run()
text = File.ReadAllText("0:\\oneos\\users.db"); //this will show the username
Console.Write(text + "> ");
var input = Console.ReadLine();
And that is if you want to make it show error put try and catch to your OS if there has an error.
if you can understand this you can make the os have user selection like windows logon screen.
If there is a problem please tell me
So I'm basically trying to make simple login or register console app but I think I can't open the text file because everytime I enter the correct username it closes the app.This is the code which I think should work.
Console.WriteLine("Username:");
string userNameN = Console.ReadLine();
string username = System.IO.File.ReadAllText(#"C:\test.txt");
if (username == userNameN)
{
Console.Clear();
Console.WriteLine("Correct username");
}
else
{
Console.WriteLine("Incorrect username program will close");
System.Threading.Thread.Sleep(1000);
Environment.Exit(0);
}
System.Threading.Thread.Sleep(1000);
It is happening because execution of code is ended. If you want to wait till next input then add Console.ReadLine(); after Console.WriteLine("Correct username");
Now it will wait till next user input.
EDIT:
My problem has been solved thanks to the user Chris Larabell, thank you to all that responded.
The issue that is happening with my code is that when the said file is not present in the Desktop directory, the console will close and will not go to the else statement for what happens when the file is not present. When the file is present however, the console will work completely fine, it is just the else statement.
Here is my code that is being used.
if (inputDrive == "search.system")
{
try
{
string Desktop = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
string DeleteFile = #"delete.txt";
string[] fileList = System.IO.Directory.GetFiles(Desktop, DeleteFile);
foreach (string file in fileList)
{
if (System.IO.File.Exists(file))
{
System.IO.File.Delete(file);
Console.WriteLine("File has been deleted");
Console.ReadLine();
}
else
{
Console.Write("File could not be found");
Console.ReadLine();
}
}
}
catch (System.IO.FileNotFoundException)
{
Console.WriteLine("search has encountered an error");
Console.ReadLine();
}
}
What I am trying to accomplish is to find a file through the Desktop directory with the name of 'delete.txt' and to delete it when the user enters "search.system". the console would then say back to you that the file has been deleted. If the file has not been found, it would say that "the file could not be found" back to you through console. If an error would to occur, it would go to catch and say "search has encountered an error"
I also want to say that I am sorry if this code is messy and/or if this is completely wrong from what I am trying to accomplish. I am new to C#, and new to coding in general.
You would want to put an if statement to check that the fileList length is > 0. If the file length is zero, the file was not found. Otherwise, you can proceed to delete the file.
Also, don’t be discouraged as a new coder. Set a breakpoint at the line where you use the GetFiles() method and step (F11) to the next line. Hover your cursor over the fileList variable and see if the number of items in the array is zero.
System.IO.Directory.GetFiles()
It looks like you are simply looking for a specific file by name and deleting it if it exists. You could simplify your code by doing this:
if (inputDrive == "search.system")
{
try
{
string Desktop = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
string DeleteFile = #"delete.txt";
string filePath = System.IO.Path.Combine(Desktop, DeleteFile);
if (System.IO.File.Exists(filePath))
{
System.IO.File.Delete(filePath);
Console.WriteLine("File has been deleted");
Console.ReadLine();
}
else
{
Console.Write("File could not be found");
Console.ReadLine();
}
}
catch (System.Exception ex)
{
Console.WriteLine($"search has encountered an error: {ex}");
Console.ReadLine();
}
}
I'm trying to set up an mIRC bot for my channel, and my current project is playing an audio file on my computer whenever a certain event happens.
I've written a short C# console app that gets the name of the file from the arguments, and plays that file.
This works running it from cmd or using a shortcut, but when I enter the command on my channel, the program comes up, but throws a FileNotFound exception.
I wrote some code using try{} catch{} to see exactly what's happening. In the event that the file fails to play, it will first list the argument that was provided, the extension (I'm going to change this later), and finally the combined string. What it comes up with it this:
args[0]: audiofile
extension: .wav
filename: audiofile.wav
Which is exactly what the file name is, and that plays perfectly from the command line.
Does anybody know what's going on here?
static void Main(string[] args)
{
string extension = ".wav";
string filename = "null";
if (args == null || args.Length == 0)
{
Console.WriteLine("No arguments provided!");
Console.ReadLine();
return;
}
filename = args[0] + extension;
Console.Write("Press enter to play grenade... ");
Console.ReadLine();
try
{
Console.WriteLine("Playing file " + filename);
(new SoundPlayer(filename)).Play();
}
catch
{
Console.WriteLine("Error!");
Console.WriteLine("args[0]: " + args[0]);
Console.WriteLine("extension: " + extension);
Console.WriteLine("filename: " + filename);
}
Console.ReadLine();
}
mIRC script:
on $*:text:!grenade:#: {
/run "c:/users/electrospeed/documents/visual studio 2013/projects/audioplayer/audioplayer/bin/debug/audioplayer.exe" audiofile
}
At the mIRC code, you wrote audiofile at the end, correct me if i'm wrong, but I guess you meant %audiofile like in variable.
I don't think you need the quotation marks in the directory path.