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.
Related
Im having an issue with using DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Open is not opening a spreadsheet, it returns a file not found exception. The class i'm using has worked many times before, but i've never used it in a UWP project.
I've created a simple example and found that I get the same issue when using File.Exists i've include all the using statements i use if that helps.
Does anyone know why the File.Exists cannot detect the file?
and yes i've triple checked the file does exist on D:!
C# UWP Project created using UWP Template Studio [MainPage.xaml.cs]
using System;
using System.IO;
using System.Data;
using System.Linq;
using System.Diagnostics;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Windows.UI.Xaml;
using UWP_APP.ViewModels;
using Windows.UI.Xaml.Controls;
namespace UWP_APP.Views
{
public sealed partial class MainPage : Page
{
public MainViewModel ViewModel { get; } = new MainViewModel();
public MainPage()
{
InitializeComponent();
string filePath = #"D:\example.xlsm";
if (File.Exists(filePath))
{
int a = 1;
}
else
{
int a = 0;
}
}
Does anyone know why the File.Exists cannot detect the file?
UWP app is running in sandbox, because File.Exists is System.IO api. So it could not work for accessing file except ApplicationData.Current.LocalFolder. If you do want to check if the file exist in the specific path, we suggest you add broadFileSystemAccess capability and enable in the system file access setting. This capability works for APIs in the Windows.Storage namespace.
And using the flolowing method to check if the file exist.
try
{
var file = StorageFile.GetFileFromPathAsync(#"C:\Users\Karan\OneDrive\Desktop\2010.pdf");
if (file != null)
{
isExist = true;
}
}
catch (Exception)
{
isExist = false;
}
today we were trying to transfer files with using C# but there are some problems here i don't really know what is wrong
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace FileTransfer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string curDir = Directory.GetCurrentDirectory();
var userDir = new DirectoryInfo(Environment.GetFolderPath(System.Environment.SpecialFolder.UserProfile));
string path = curDir + #"\1.jar";
string path2 = userDir + #"\Desktop\2.jar";
File.Move(path, path2);
}
}
}
It says 1.jar file cannot found.
Try this:
string path = Path.Combine(curDir,"1.jar");
string path2 = Path.Combine(userDir,"Desktop","2.jar");
//if this still doesn't work, put a breakpoint after these two lines
When your breakpoint is hit look at the value of path. Is there a even file at that location? If not, you need build your path correctly.
Also, it would be a good idea to check if the file exists before you try to move it:
if(!File.Exists(path))
{
//handle this scenario here
return;
}
File.Move(path,path2);
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 . . .
I am attempting to call a Web Service (created in PHP) from my C# application. I have successfully added the Web Reference in Visual Studios, however I cannot figure out exactly how to invoke the call to the web service. I have been following this tutorial: http://sanity-free.org/article25.html however when I try and compile I get a "The type or namespace name 'SimpleService' could not be found". My C# code is as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace inVision_OCR
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void translateButton_Click(object sender, EventArgs e)
{
// We need to snap the image here somehow . . .
// Open up the image and read its contents into a string
FileStream file = new FileStream("\\Hard Disk\\ocr_images\\test.jpg", FileMode.Open);
StreamReader sr = new StreamReader(file);
string s = sr.ReadToEnd();
sr.Close();
// Using SOAP, pass this message to our development server
SimpleService svc = new SimpleService();
string s1 = svc.getOCR("test");
MessageBox.Show(s);
}
}
}
The SimpleService is probably in a different namespace, look at the properties of the web reference you added and see what namespace the proxy class SimpleService is being generated in, then add a using statement in your code to reference that namespace.
I am currently working on batch processing application using MSMQ in C#. In the application design, I have an error queue containing XML messages using the ActiveXFormatter. I know that I can write an application to write these error messages to text files or database tables.
Are there other pre-built tools available allowing you to export messages to variety of formats (i.e. text files, database tables, etc.)? I am just looking for best practices.
Ok. I found the solution of writing code to be really easy. Here's my reference solution.
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.Messaging;
namespace ExportMSMQMessagesToFiles
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void btnExportTextFiles_Click(object sender, EventArgs e)
{
//Setup MSMQ using path from user...
MessageQueue q = new MessageQueue(txtMSMQ.Text);
//Setup formatter... Whatever you want!?
q.Formatter = new ActiveXMessageFormatter();
// Loop over all messages and write them to a file... (in this case XML)
MessageEnumerator msgEnum = q.GetMessageEnumerator2();
int k = 0;
while (msgEnum.MoveNext())
{
System.Messaging.Message msg = msgEnum.Current;
string fileName = this.txtFileLocation.Text + "\\" + k + ".xml";
System.IO.File.WriteAllText(fileName, msg.Body.ToString());
k++;
}
MessageBox.Show("All done!");
}
}
}