C# 开发PDA程序,在调试状态下提示找不到***.xml文件,但是生成EXE是没有任何问题是什么原因呀

.Net技术 码拜 8年前 (2016-05-18) 1078次浏览
完整代码如下【XML读写类】:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Collections;
using System.Reflection;
namespace XyPDA
{
public class gpsXMLSettingReader
{
private static Hashtable m_hsTable = null;
private static gpsXMLSettingReader sysSettingReader = null;
private gpsXMLSettingReader()
{
try
{
this.DoXMLReader();
}
catch (Exception exception)
{
throw exception;
}
}
private void DoXMLReader()
{
//this.ReadConfigData();
}
private static string GetConfigXMLFile()
{
//string strPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase.ToString()) + “\U8Setting.xml”;
//string strPath = @”U8Setting.xml”;
//string strPath = Path.Combine(“D:\OtherSecondaryDevelopment\Xy003\XyPDA\XyPDA\bin\Debug\U8.xml”, “”);
string strPath = @”U8.xml”;
return strPath;
}
public static gpsXMLSettingReader getInstance()
{
try
{
if (sysSettingReader == null)
{
m_hsTable = new Hashtable();
sysSettingReader = new gpsXMLSettingReader();
}
}
catch (Exception exception)
{
throw exception;
}
return sysSettingReader;
}
public static string GetSettingConfig(string pro, string par)
{
string str = “”;
//string configXMLFile = GetConfigXMLFile();
//XmlTextReader reader = new XmlTextReader(configXMLFile);
//reader.MoveToContent();
//XmlDocument document = new XmlDocument();
//document.LoadXml(reader.ReadOuterXml());
string configXMLFile = GetConfigXMLFile();
XmlDocument document = new XmlDocument();
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreComments = true;
XmlReader reader = XmlReader.Create(configXMLFile, settings);
document.Load(reader);
try
{
str = document.FirstChild.SelectSingleNode(pro).SelectSingleNode(par).InnerText;
reader.Close();

}
catch (Exception exception)
{
throw exception;
}
return str;
}
public static void SetSettingConfig(string pro, string par, string val)
{
string configXMLFile = GetConfigXMLFile();
XmlTextReader reader = new XmlTextReader(configXMLFile);
reader.MoveToContent();
XmlDocument document = new XmlDocument();
document.LoadXml(reader.ReadOuterXml());
try
{
XmlNode node = document.FirstChild.SelectSingleNode(pro).SelectSingleNode(par);
if (node != null)
{
node.InnerText = val;
}
reader.Close();
document.Save(configXMLFile);
}
catch (Exception exception)
{
throw exception;
}
}
}
}
调用代码如下:
this.textBox1.Text = gpsXMLSettingReader.GetSettingConfig(“config”, “ServerIP”); //读取服务器
this.textBox2.Text = gpsXMLSettingReader.GetSettingConfig(“config”, “UserID”); //读取用户名
出现问题图片如下[C# 开发PDA程序,在调试状态下提示找不到***.xml文件,但是生成EXE是没有任何问题是什么原因呀?]
C# 开发PDA程序,在调试状态下提示找不到***.xml文件,但是生成EXE是没有任何问题是什么原因呀

解决方案

2

你断点看下configXMLFile文件的路径是不是正确的。本人看到你的xml文件路径是“\UB.xml”。假如xml文件和运行程序是在同一目录的话应该是“UB.xml”,没有那个“\”。全部肯定是你给的路径有问题。

2

应该是路径不对,你断点看一下路径,然后把路径复制出来,粘贴到本人的电脑的地址栏上,假如找不到那个文件就是路径的问题了

46

你在VS中右键 xml文件,然后在属性中选择“始终复制”
winform程序是生成在bin对应目录下的,对于xml这种东西默认是不复制的

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C# 开发PDA程序,在调试状态下提示找不到***.xml文件,但是生成EXE是没有任何问题是什么原因呀
喜欢 (0)
[1034331897@qq.com]
分享 (0)