在写一个窗体控件时,在它的构造函数里面用相对路径访问一个配置文件,但在设计时,将此控件拉到窗体上时,就报告找不到配置文件!
当然,这本应该就找不到配置文件,因为现在的运行目录是IDE的运行目录,所以在IDE的运行目录下肯定找不到放在我的应用程序目录的配置文件!
如此一来,就像找不到解决方法了似的!
但你再看 .NET里面自带的应用程序设置(就是通过Perperties.Settings.Default.具体属性 访问的那个),当你在一个控件里用这些设置时,在设计时仍然能正常访问,而它的配置文件也在应用程序目录里面!它是怎么做到的呢?
控件代码:public class myControl:UserControl
{public myControl
{
  using(StreamReader sr=new StreamReader("profile.ini"))
{
  this.Tag=sr.ReadLine();
}  
//other codes
}
}哪位朋友能指点指点,不胜感激!

解决方案 »

  1.   

    LZ Application.StartupPath+文件名 试试
      

  2.   

    设计时,别让它执行访问文件之类的代码.if (ComponentModel)

       // 在此写在设计模式下,需要执行的代码
    }
    else    //在运行模式下执行
    {
       using(StreamReader sr=new StreamReader("profile.ini"))
       {
          this.Tag=sr.ReadLine();
       }  
    }
      

  3.   

    Application.StartupPath+"\profile.ini"
      

  4.   

    Application.StartupPath
    如果不是动态加载插件 等
    建议这么用但是对于插件系统 最好通过HelpEntityClass 帮助加载
    相对比较安全
      

  5.   

    LZ可以看下Application、Path帮助文档。
      

  6.   

    winform里就是用Application.StartupPath + "\\profile.INI"
      

  7.   


    Application.StartupPath
    如果不是动态加载插件 等
    建议这么用但是对于插件系统 最好通过HelpEntityClass 帮助加载
    相对比较安全
      

  8.   

    Application.StartPath在设计时的值依然是IDE的路径!
    而现在需要的是在设计时获得应用程序的路径!
      

  9.   

    Quote=引用 2 楼 lzsh0622 的回复:]
    设计时,别让它执行访问文件之类的代码.if (ComponentModel)
    {// 在此写在设计模式下,需要执行的代码}else//在运行模式下执行{using(StreamReader sr=new StreamReader("profile.ini"))
       {this.Tag=sr.ReadLine();
       }  
    }[
    [/Quote]
    lzsh0622 想说的是不是这样写if(!DesignMode)
    {
    //访问数据
    }
      

  10.   

    public partial class myControl : UserControl
    {
        public myControl()
        {
            InitializeComponent();        if (!(this.DesignMode))
            {
                using (StreamReader sr = new StreamReader("profile.ini"))
                {
                    this.Tag = sr.ReadLine();
                }
            }    }
    }
      

  11.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.IO;
    using EnvDTE; // 要先添加引用
    using EnvDTE80; // 要先添加引用namespace WindowsFormsControlLibrary2
    {
        public partial class UserControl1 : UserControl
        {
            public UserControl1()
            {
                InitializeComponent();            DTE2 D2 = (DTE2)Marshal.GetActiveObject("VisualStudio.DTE");
                String SlnName = D2.Solution.FullName;
                String DllPath = Path.GetDirectoryName(SlnName);
                // 显示当前包含这个控件的那个工程所在的解决方案所在目录
                // 而且设计时和运行时都是返回一样的
                // 只要把配置文件放在这个目录里就可以在运行时就调用
                // 比如 DllPath 会是
                // C:\Documents and Settings\Administrator\My Documents
                // \Visual Studio 2008\Projects\WindowsFormsApplication74
                MessageBox.Show(DllPath);
            }
        }
    }