在cs类文件里写:
System.Web.HttpContext.Current.Server.MapPath("a.txt").ToString();  
报错“未将对象引用设置到对象的实例。”
我不知道为什么,

解决方案 »

  1.   

    我查了,可能是因为没有Web.HttpContext引起的,要是在页面文件里就没有问题
    可是现在是在自定义类里的
      

  2.   

    如果需要是WEB应用的目录下就很好办啊。假设web根目录为:c:\www,而DB放在www目录下的话则可以这样。 
    System.AppDomain.CurrentDomain.BaseDirectory.ToString() + ConfigurationManager.AppSettings["dbPath"]就可以了
    我找到办法了,就是上面的
      

  3.   

    你的1.txt是否存在于指定目录?
    FILE.EXIST(1.TXT)他是BOOL型的 你看下返回值
      

  4.   


    //此处写你自己想执行的任务
                    string path = System.Web.HttpContext.Current.Server.MapPath("a.txt").ToString();  
                    //string path = "D:\\web\\code\\定时\\a.txt";
                    zhqlzzf.FileObj.WriteFile(path, DateTime.Now.ToString());
    上面是错误 的代码,
    FILE.EXIST(1.TXT)没有问题,
      

  5.   

    我使用过   
    string path = System.Web.HttpContext.Current.Server.MapPath("a.txt").ToString();
    File.WriteAllText(path,context);
    在write的时候出错了,说找不到文件。
    楼上的知道是怎么原因吗? 
      

  6.   

    在多线程里面使用HttpContext.Current,HttpContext.Current是得到null的.这么用:
            public static string MapPath(string strPath)
            {
                if (HttpContext.Current != null)
                {
                    return HttpContext.Current.Server.MapPath(strPath);
                }
                else //非web程序引用
                {
                    strPath = strPath.Replace("/", "\\");
                    if (strPath.StartsWith("\\"))
                    {
                        //strPath = strPath.Substring(strPath.IndexOf('\\', 1)).TrimStart('\\');
                        strPath = strPath.TrimStart('\\');
                    }
                    return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
                }
            }
      

  7.   

    winner2050的方法在discuz.net里用的,但是不知道怎么模拟HttpContext