Trying to access Settings.Settings from second Project - c#

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 . . .

Related

ModifierKeys does not exist, despite System.Windows.Input being used

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.

Visual Studio execution closes when pressing enter

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.

fetching only one url in c#

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.

C# program that will go through a web.config file

I am writing a C# program that will check to ensure that all the connections it should have active based on it's web config file are active and if not it till try to restart the connection and tell the host if it fails or passes at doing so.
I have very little understanding of the web.config file I know it's XML and I think the point's I want to see are active are end points.
currently I am able to read the file but not able to just get the test after the "endpoint="
The idea/goal of the program is to let me be able to restart a connection form my web app to my database if for some reason it dropped and to let me know that I while i run this program that the connection was down.
Program.cs
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Forms;
namespace webconfig
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Form 1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace webconfig
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void richTextBox1_TextChanged(object sender, EventArgs e)
{
RTBconsole.Text = "" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/" + DateTime.Now.Year + "\r\n\r\n";
// create reader & open file
string strFilename = "C:\\Sites\\EasyeServeIDSrv\\Web.config";
FileStream fsVideos = new FileStream(strFilename,FileMode.Open,FileAccess.Read);
System.Xml.XmlTextReader rdrXml = new System.Xml.XmlTextReader(fsVideos);
StreamReader tr = new StreamReader("C:\\Sites\\EasyeServeIDSrv\\Web.config");
do
{
String current = tr.ReadLine();
// read a line of text
if (current.Contains("endpoint") == false || current.Contains("</endpoint>") == false)
{
RTBconsole.AppendText(" "+ current.ToString());
}else{
}
}while(!tr.EndOfStream);
do
{
// Read an item and return true
// Continue reading as long as ...
} while (rdrXml.Read() == true); // ... as long as Read() returns true
// Once Read() returns false, STOP!!!
fsVideos.Close();
Console.WriteLine();
// close the stream
tr.Close();
}
}
}
Each section of the web.config corresponds to a class inheriting from the ConfigurationSection class. Something like this should allow you to read each section of the web.config:
//get the configuration file
Configuration config = WebConfigurationManager.OpenWebConfiguration("..."); //path to config
//get smtpConfiguration section
ConfigurationSection section = config.GetSection("smtpConfiguration");
You should look at using LINQ to XML to query your web.config for elements of interest. Here is an example of a person writing to the web.config using LINQ to XML.
It's always better to manipulate the web.config using the namespace System.configuration.
There are some classes in thera that reads/writes the connection strings and app settings on execution.

Registry Problem

I made a launcher for my game server. (World of Warcraft)
I want to get the installpath of the game, browsed by the user.
I'm using this code to browse, and get the installpath, then set some other strings from the installpath string, then just strore in my registry key.
using System;
using System.Drawing;
using System.Reflection;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.Win32;
using System.IO;
using System.Net.NetworkInformation;
using System.Diagnostics;
using System.Runtime;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Cryptography;
using System.Text;
using System.Net;
using System.Linq;
using System.Net.Sockets;
using System.Collections.Generic;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string InstallPath, WoWExe, PatchPath;
private void Form1_Load(object sender, EventArgs e)
{
RegistryKey LocalMachineKey_Existence;
MessageBox.Show("Browse your install location.", "Select Wow.exe");
OpenFileDialog BrowseInstallPath = new OpenFileDialog();
BrowseInstallPath.Filter = "wow.exe|*.exe";
if (BrowseInstallPath.ShowDialog() == DialogResult.OK)
{
InstallPath = System.IO.Path.GetDirectoryName(BrowseInstallPath.FileName);
WoWExe = InstallPath + "\\wow.exe";
PatchPath = InstallPath + "\\Data\\";
LocalMachineKey_Existence = Registry.LocalMachine.CreateSubKey(#"SOFTWARE\ExistenceWoW");
LocalMachineKey_Existence.SetValue("InstallPathLocation", InstallPath);
LocalMachineKey_Existence.SetValue("PatchPathLocation", PatchPath);
LocalMachineKey_Existence.SetValue("WoWExeLocation", WoWExe);
}
}
}
}
The problem is:
On some computer, it doesnt stores like it should be. For example, your wow.exe is in C:\ASD\wow.exe, your select it with the browse windows, then the program should store it in the Existence registry key as C:\ASD\Data\ but it stores like this:
C:\ASDData , so it forgots a backslash :S
Look at this picture:
http://img21.imageshack.us/img21/2829/regedita.jpg
My program works cool on my PC, and on my friends pc, but on some pc this "bug" comes out :S
I have windows 7, with .NEt 3.5
Please help me.
Can you debug and see what InstallPath contains?
Try it with Path.Combine instead of string concatenation, e.g.:
WowExe = Path.Combine(InstallPath, "wow.exe");
PatchPath = Path.Combine(InstallPath, #"\Data\");

Categories

Resources