G
G
gentleactive2019-04-23 22:51:19
C++ / C#
gentleactive, 2019-04-23 22:51:19

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>

program code
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)
        {
            
        }
    }
}

Run through
private void button1_Click(object sender, EventArgs e)
        {
            Process.Start("C:\Users\1234\Desktop\start.bat");
        }

does not suit me because I would like to be able to change the location of the file already in the compiled program!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
Foggy Finder, 2019-04-24
@gentleactive

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>

I can't say that I like this xml structure, but at least now I can work with it.
To read the path:
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('"')); // запускаем процесс
}

H
Harlan, 2019-04-24
@Harlan

What about the view code

string prg = ...; // Читаете путь к bat-файлу из XML
Process.Start(prg);

disgusts the good sir?
The second option: you can run cmd.exe with /C parameters and the path to the bat file. Those. launch will look like this:
Process.Start(@"C:\Windows\System32\cmd.exe", "/C " + prg);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question