Hello im trying to learn C# step by step. I installed Visual Studio to practice but 20 mins in I cant test my basic code when executing:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.Write("What is your name?");
string name = Console.ReadLine();
Console.WriteLine("My name is " + name);
}
}
}
It is as basic as this but when I execute and type a name and press enter, the cmd just closes. any help would be appreciated because i am enthusiastic to start out with C#
cmd
The Main method returns after
Console.WriteLine("My name is " + name);
And this effectively terminates the app.
You should put a
Console.Read();
to wait until the next keystroke.
Add another input to close application, so cmd wont close until you press enter again.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.Write("What is your name?");
string name = Console.ReadLine();
Console.WriteLine("My name is " + name);
Console.ReadLine();
}
}
}
Try Ctrl + F5
If you don't need to debug, then Ctrl-F5 is the best option
works automatically without any Console.Readline() or ReadKey()
Visual Studio will keep the console window open, until you press a key.
Related
This is my current code, it works but it restarts the Explorer process and that's kinda weird.
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Win32;
namespace MyApp
{
class Program
{
static async Task Main(string[] args)
{
ShowSmallTaskbarIcons();
// ShowLargeTaskbarIcons();
Console.WriteLine("Press a key to exit...");
Console.ReadKey();
}
static void ShowSmallTaskbarIcons()
{
Registry.SetValue(#"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced", "TaskbarSmallIcons", "1", RegistryValueKind.DWord);
RefreshExplorer();
}
static void ShowLargeTaskbarIcons()
{
var key = Registry.CurrentUser.OpenSubKey(#"Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced", writable: true);
key.DeleteValue("TaskbarSmallIcons", throwOnMissingValue: false);
key.Close();
RefreshExplorer();
}
static void RefreshExplorer()
{
Process.Start("taskkill.exe", "/f /im explorer.exe");
Thread.Sleep(2000);
Process.Start("explorer.exe");
Thread.Sleep(10000);
}
}
}
I would like to do the same as with the "Use small taskbar buttons" toggle switch from Settings.
How to do the same with C#?
How to change Windows 10 taskbar icon size programmatically
This post has an example about how to send WM_SETTINGCHANGE message in c++.
However, there is no easy way to do it in c# wrapped function. You have to do it through p/invoke
Following post will help you do it in c#
convert C++ code to C#: SendMessageTimeout()
I'm trying to make a bot that uses OCR and I'm having issues trying to get a hotkey library I found to work.
It uses the ModifierKeys enum as an argument in one of it's functions but apparently 'ModifierKeys doesn't exist'.
I am using System.Windows.Input which should have ModifierKeys in it and I have double checked that I have System.Windows referenced in my project (although that should be pretty obvious since I get no error to do with using System.Windows.Input, I guess)
Here's my current code (error is happening at var key):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using mrousavy;
using System.Windows.Input;
using OCRBot.Handlers;
namespace OCRBot
{
class Program
{
static OCRHandler oCRHandler = new OCRHandler();
static void Main(string[] args)
{
Console.Write("!!");
#if DEBUG
Console.WriteLine("\nPress enter to close...");
Console.ReadLine();
#endif
var key = new HotKey(
(ModifierKeys.Control | ModifierKeys.Alt),
Key.S,
this,
delegate {
MessageBox.Show("Ctrl + Alt + S was pressed!");
}
);
while (true)
{
MainLoop();
}
}
static void MainLoop()
{
oCRHandler.ReadWindow();
}
}
}
The Reference you want is WindowsBase to get ModifierKeys, not System.Windows.
Problem Summary
I have two projects (Console Application and Settings Console). I want "Console Application" to access "Settings Console's" Settings.settings file and use that data in the program.
What I've Done
I've already set the Settings.Settings Access Modifier to "Public", and Have set the Scope of each setting to "User"
Settings Console Output:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics;
namespace Settings_Console
{
class Program
{
static void Main(string[] args)
{
//Show Stored Settings
Console.WriteLine(Properties.Settings.Default.Name + "" + Properties.Settings.Default.Number);
String name, number;
name = Console.ReadLine();
number = Console.ReadLine();
Properties.Settings.Default.Name = name;
Properties.Settings.Default.Number = number;
Properties.Settings.Default.Save();
//Show Settings Changed into
Console.WriteLine(Properites.Settings.Default.Name + "" + Properties.Settings.Default.Number);
}
}
}
Settings Console Output:
Amanda 9568675309
Sergio
9568254235
Sergio 9568254235
Press any key to continue . . .
Console Application Project:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System_Console;
namespace Console_Application
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Settings_Console.Properties.Settings.Default.Name + "" + Settings_Console.Properties.Settings.Default.Number);
}
}
}
Console Application Output:
Press any key to continue . . .
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
namespace SchoolPasswordLockFolder
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Enter the password: "); // the space character after the semicolon has an error
public string input = Console.ReadLine();
}
}
}
The errors:
Severity Code Description Project File Line
Error CS1513 } expected SchoolPasswordLockFolder c:\Users\CENSOREDSIJGIOFSGJIOFS\documents\visual studio 2015\Projects\App5\SchoolPasswordLockFolder\SchoolPasswordLockFolder\Program.cs 14
(for the one after the semicolon)
and
Severity Code Description Project File Line
Error CS1022 Type or namespace definition, or end-of-file expected SchoolPasswordLockFolder c:\Users\CENSOREDIDONTWANTSTALKERS\documents\visual studio 2015\Projects\App5\SchoolPasswordLockFolder\SchoolPasswordLockFolder\Program.cs 19
(for the last bracket)
I have not programmed in C# for a very long time as I was too busy with web development and lua...
change this:
public string input = Console.ReadLine();
to:
string input = Console.ReadLine();
local variables do not get accessibility modifiers like public.
I am working on collecting urls from the web site in C# using WatiN framework. In my program it is fetching only one url. I don't know what is the problem. Any help will be appreciated.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WatiN.Core;
using WatiN.Core.Native.InternetExplorer;
namespace magicbricks
{
class scroll
{
[STAThread]
static void Main(string[] args)
{
Browser browserInstance;
browserInstance = new IE(#"http://www.99acres.com/property-in-chennai- ffid?search_type=QS&search_location=CP32&lstAcn=CP_R&lstAcnId=32&src=CLUSTER&isvoicesearch=N&keyword_suggest=chennai%20%28all%29%3B&fullSelectedSuggestions=chennai%20%28all%29&strEntityMap=W3sidHlwZSI6ImNpdHkifSx7IjEiOlsiY2hlbm5haSAoYWxsKSIsIkNJVFlfMzIsIFBSRUZFUkVOQ0VfUywgUkVTQ09NX1IiXX1d&texttypedtillsuggestion=chennai&refine_results=Y&Refine_Localities=Refine%20Localities&action=%2Fdo%2Fquicksearch%2Fsearch&suggestion=CITY_32%2C%20PREFERENCE_S%2C%20RESCOM_R");
foreach (var links in browserInstance.Links.Filter(Find.ByClass("b")))
{
Console.WriteLine(links.Url);
String filePath = "C:/Users/User/Desktop/New folder";
String fileName = "newop4.csv";
using (StreamWriter sr = new StreamWriter(Path.Combine(filePath, fileName), true))
{
sr.WriteLine(links.Url);
}
Console.ReadLine();
}
}
}
}
the above code prints only one url in the console.
Remove the Console.ReadLine(); As you are in a ForEach loop. If you still want the Console.ReadLine(); move it out the foreach
The Console.ReadLine(); waits for a user input, after you enter any value you should see the next URL.