Answer the question
In order to leave comments, you need to log in
C# By clicking on the button, start.bat is launched, the path where start.bat is located is read from Settings.xml, How to implement this?
Good afternoon.
Tell me how to implement?
it is necessary that when button1_Click is pressed, start.bat is launched, but so that the path where start.bat itself lies is written in Settings.xml lying at the root with the program, give an example of how to implement it, I just learn how I can’t understand what and where to write
There are Settings. xml
<?xml version="1.0" encoding="utf-8" ?>
<Settings>
<PathToFile>"C:\Users\1234\Desktop\start.bat"</PathToFile>
</Settings>
<Settings>
<PathToFile>"C:\Users\1234\Desktop\start2.bat"</PathToFile>
</Settings>
namespace Launcher
{
public partial class Form1 : Form
{
private KeyEventArgs e;
public Form1()
{
InitializeComponent();
this.KeyPreview = true;
this.IsMdiContainer = false;
logo sf = new logo();
sf.ShowDialog();
Time.Text = "";
timer1.Enabled = true;
timer1.Interval = 1000;
}
protected override void OnKeyDown(KeyEventArgs a)
{
base.OnKeyDown(a);
if (a.KeyCode == Keys.F4 && a.Alt)
{
//MessageBox.Show("Test");
a.Handled = true;
}
}
private void btnExit_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.Show();
}
private void timer1_Tick(object sender, EventArgs e)
{
Time.Text = DateTime.Now.ToLongTimeString();
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
private void button1_Click(object sender, EventArgs e)
{
Process.Start("C:\Users\1234\Desktop\start.bat");
}
Answer the question
In order to leave comments, you need to log in
To read information from XML, you can use Linq2Xml, XmlSerializer, XmlDocument, or something else.
In your case, in my opinion, Linq2Xml is better suited, since the source file is small and information needs to be extracted from it whenever requested.
There is an online tutorial in Russian:
Selecting elements in LINQ to XML
Understanding LINQ to XML (C#)
The xml given in the question is not valid - it contains more than one root element (Settings), add root to it:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<Settings>
<PathToFile>"C:\Users\1234\Desktop\start.bat"</PathToFile>
</Settings>
<Settings>
<PathToFile>"C:\Users\1234\Desktop\start2.bat"</PathToFile>
</Settings>
</root>
var path = "Settings.xml"; // путь к файлу настроек
var batName = "start.bat";
var xdoc = XDocument.Load(path);
var batPath =
xdoc
.Descendants("PathToFile") // ищем элементы PathToFile
.SingleOrDefault(xe => xe.Value.Contains(batName)); // ищем единственный из них содержащий нужный bat файл
if (batPath == null) // если в файле нет нужного пути
{
MessageBox.Show("Путь не указан"); // сообщаем пользователю
}
else // иначе
{
Process.Start(batPath.Value.Trim('"')); // запускаем процесс
}
What about the view code
string prg = ...; // Читаете путь к bat-файлу из XML
Process.Start(prg);
Process.Start(@"C:\Windows\System32\cmd.exe", "/C " + prg);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question