我有一个xml,我保留了一些图像和一些exe的路径。 我需要我的程序来读取xml并创建与元素一样多的按钮,为每个按钮分配其图像并为其提供运行.exe的按钮
我的程序读取xml并创建按钮。我需要按钮来获取图像以及.exe运行时.exe
我的班级阅读xml
using System.IO;
using System.Xml.Serialization;
public class XmlManager {
private string xmlPath;
public XmlManager(string xmlPath) {
this.xmlPath = xmlPath;
}
public Datos ReadXmlTest() {
XmlSerializer serializer = new XmlSerializer(typeof(Datos));
StreamReader reader = new StreamReader(xmlPath);
Datos data = (Datos)serializer.Deserialize(reader);
reader.Close();
return data;
}
}
我的班级生成按钮并将de image放入按钮
using System.IO;
using UnityEngine;
using UnityEngine.UI;
public class AppLogic : MonoBehaviour {
[SerializeField]
private Transform layout;
[SerializeField]
private Button buttonPrefab;
private Datos data;
void Awake() {
string path = "C:/Users/datos.xml";
XmlManager xmlMng = new XmlManager(path);
data = xmlMng.ReadXmlTest();
foreach (var juego in data.Juegos) {
Button newButton = Instantiate(buttonPrefab);
newButton.transform.SetParent(layout);
newButton.GetComponent<AppButton>();
Sprite imageSprite = new Sprite();
Texture2D SpriteTexture = Texture(path);
imageSprite = Sprite.Create(SpriteTexture, new Rect(0, 0, SpriteTexture.width, SpriteTexture.height), new Vector2(0, 0), 100.0f);
newButton.image.sprite = imageSprite;
}
}
public Texture2D Texture(string path) {
Texture2D Texture2D;
byte[] FileData;
if (File.Exists(path)) {
FileData = File.ReadAllBytes(path);
Texture2D = new Texture2D(1, 1);
if (Texture2D.LoadImage(FileData))
return Texture2D;
}
return null;
}
}
我的xml文件
<?xml version="1.0" encoding="utf-8"?>
<Datos>
<dato>
<play>
<ruta>D:/exe.exe</ruta>
<img>C:/png.png</img>
</play>
<play>
<ruta>D:/exe1.exe</ruta>
<img>C:/png1.png</img>
</play>
</dato>
</Datos>
创建按钮时我的程序放置一个默认的统一图像。我认为这是因为它读取所有xml而不仅仅是图像。
我希望你理解我,我是西班牙语jejeje
答案 0 :(得分:3)
如果您想从C#运行.exe
,则应使用System.Diagnostics
有了它,您可以创建一个新的Process
并启动.exe
此示例来自msdn文档
using System;
using System.Diagnostics;
using System.ComponentModel;
namespace MyProcessSample
{
class MyProcess
{
public static void Main()
{
Process myProcess = new Process();
try
{
myProcess.StartInfo.UseShellExecute = false;
// You can start any process, HelloWorld is a do-nothing example.
myProcess.StartInfo.FileName = "C:\\HelloWorld.exe";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
// This code assumes the process you are starting will terminate itself.
// Given that is is started without a window so you cannot terminate it
// on the desktop, it must terminate itself or you can do it programmatically
// from this application using the Kill method.
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}