当前位置:编程学习 > C#/ASP.NET >>

出错未能找到路径 'C:\Program Files \Microsoft Visual Studio10.0\Common7\IDE\xml\C.xml

    小弟在做个winform项目,有地方需要载入XML
写了一些方法但是出错
Warning 16 Could not find file 'C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\xml\CameraInfo.xml'. 0 0
   

具体代码
        CameraUiModel model=new CameraUiModel() ;
        CameraInfo cameraInfo = new CameraInfo();
        public ucCamera()
        {
            InitializeComponent();

        }
   
        private void ucCamera_Load(object sender, EventArgs e)
        {
            try
            {

                model=new CameraUiModel{
                cbxAnalysis=this.cbxAnalysis,
                cbxChannel=this.cbxChannel,
                cbxFormat=this.cbxFormat,
                cbxType=this.cbxType,
                txtCameraName=this.txtCameraName,
                txtIP=this.txtIP,
                txtAdminName=this.txtAdminName,
                txtPassWord=this.txtPassword,
                txtPort=this.txtPort,
                txtArea=this.txtArea,
                txtHeight=this.txtHeight,
                txtWidth=this.txtWidth};
             }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            InitConfig();
         }

        void InitConfig()
        {

            string filepath = Application.StartupPath + "\\xml\\CameraInfo.xml";

            ConvertXmlFileToObject converter = new ConvertXmlFileToObject(filepath);

            cameraInfo = converter.XmlToCameraInfo();

            model.cbxAnalysis.Text = cameraInfo.SystemAnalysis;
            model.cbxChannel.Text = cameraInfo.Channel;
            model.txtCameraName.Text = cameraInfo.CameraName;
            model.txtIP.Text = cameraInfo.Ip;
            model.txtAdminName.Text = cameraInfo.User;
            model.txtPort.Text = cameraInfo.Port;
            model.txtPassWord.Text = cameraInfo.Password;
            model.txtArea.Text = cameraInfo.AreaName;
            model.cbxFormat.Text = cameraInfo.Format;
            model.cbxType.Text = cameraInfo.Type;
            model.txtHeight.Text = cameraInfo.Height;
            model.txtWidth.Text = cameraInfo.Width;
        }


XML IDE Visual Studio --------------------编程问答-------------------- 求解答,求帮助 --------------------编程问答-------------------- 你确认你的计算机上有这个路径?有这个文件? --------------------编程问答-------------------- 直接把那串字符粘贴到文件路径栏里,估计应该打不开,仔细检查一下路径拼写吧 --------------------编程问答--------------------
引用 2 楼 vb763305825 的回复:
你确认你的计算机上有这个路径?有这个文件?
当然有啊,但是不知道为什么会找那个文件夹去 --------------------编程问答--------------------
引用 3 楼 UR_Not_Alone 的回复:
直接把那串字符粘贴到文件路径栏里,估计应该打不开,仔细检查一下路径拼写吧
路径是有的,只是里面没有xml的相应的文件,不明白为什么会找那个文件夹去,我已经让指向程序目录了啊 --------------------编程问答-------------------- 要不用方法获取路径吧  AppDomain.CurrentDomain.BaseDirectory  之类的 具体你搜下吧 --------------------编程问答-------------------- 是在你项目目录里的xml文件吧?
 Application.StartupPath  换成AppDomain.CurrentDomain.BaseDirectory试试 --------------------编程问答--------------------
引用 7 楼 a346729576 的回复:
是在你项目目录里的xml文件吧?
 Application.StartupPath  换成AppDomain.CurrentDomain.BaseDirectory试试
这不是差不多嘛没用啊 --------------------编程问答-------------------- 路径不正确··· --------------------编程问答--------------------
引用 9 楼 zxy397472251 的回复:
路径不正确···
什么路径不正确,C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\xml\?
这个路径是存在的,但是里面没有放.xml文件,我已经指向程序目录了啊,为什么回到这里要文件 --------------------编程问答-------------------- 求大神解答 --------------------编程问答-------------------- string filepath = Application.StartupPath + "\\xml\\CameraInfo.xml";

            ConvertXmlFileToObject converter = new ConvertXmlFileToObject(filepath);
因为这两句,你这里filepath 为 C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\xml\CameraInfo.xml,下面用new创建ConvertXmlFileToObject的时候又找不到相应的文件自然就报错了啊 --------------------编程问答--------------------
引用 12 楼 UR_Not_Alone 的回复:
string filepath = Application.StartupPath + "\\xml\\CameraInfo.xml";

            ConvertXmlFileToObject converter = new ConvertXmlFileToObject(filepath);
因为这两句,你这里filepath 为 C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\xml\CameraInfo.xml,下面用new创建ConvertXmlFileToObject的时候又找不到相应的文件自然就报错了啊
不明白,我的路径是取的程序路径啊  --------------------编程问答--------------------
string assemblyFilePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
Console.WriteLine(System.IO.Path.GetDirectoryName(assemblyFilePath));


自己看MSDN
http://msdn.microsoft.com/zh-cn/library/system.windows.forms.application.startuppath.aspx

The path for the executable file that started the application.
启动了应用程序的可执行文件的路径。

你用的VS调试的程序吧?所以可执行文件就是devenv.exe,他启动了你的应用程序app.vshost.exe
--------------------编程问答--------------------  直接用相对路径就可以了string filepath = @"./xml/CameraInfo.xml";
--------------------编程问答--------------------
引用 15 楼 DENQH 的回复:
 直接用相对路径就可以了string filepath = @"./xml/CameraInfo.xml";
还是不行,之前试过了,关键是只有camare这个Class这边出问题,还有同样做的其他类就没问题 --------------------编程问答--------------------
string filepath = @"./xml/CameraInfo.xml";
if(System.IO.File.Exists(filepath))
{
操作.....
}
else
{
提标.....
}
你先判断一下文件是否存在 --------------------编程问答-------------------- 路径获取错误了,StartupPath的问题,你可以用相对路径,C#如果是获取相对路径的话是基于生成的EXE文件而言的,就是BIN/DEBUG下的文件 --------------------编程问答--------------------
引用 13 楼 qq1792111432 的回复:
Quote: 引用 12 楼 UR_Not_Alone 的回复:

string filepath = Application.StartupPath + "\\xml\\CameraInfo.xml";

            ConvertXmlFileToObject converter = new ConvertXmlFileToObject(filepath);
因为这两句,你这里filepath 为 C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\xml\CameraInfo.xml,下面用new创建ConvertXmlFileToObject的时候又找不到相应的文件自然就报错了啊
不明白,我的路径是取的程序路径啊 

有什么不明白的,真服了你,仔细看看好么。
Application.StartupPath 这个才是取程序路径,你后面不是还有 + "\\xml\\CameraInfo.xml"么。加上以后就变成文件全路径了啊
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,