System.Web.HttpContext.Current.Server.MapPath("a.txt").ToString(); 报错“未将对象引用设置到对象的实例。” 在cs类文件里写:System.Web.HttpContext.Current.Server.MapPath("a.txt").ToString(); 报错“未将对象引用设置到对象的实例。”我不知道为什么, 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我查了,可能是因为没有Web.HttpContext引起的,要是在页面文件里就没有问题可是现在是在自定义类里的 如果需要是WEB应用的目录下就很好办啊。假设web根目录为:c:\www,而DB放在www目录下的话则可以这样。 System.AppDomain.CurrentDomain.BaseDirectory.ToString() + ConfigurationManager.AppSettings["dbPath"]就可以了我找到办法了,就是上面的 你的1.txt是否存在于指定目录?FILE.EXIST(1.TXT)他是BOOL型的 你看下返回值 //此处写你自己想执行的任务 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)没有问题, 我使用过 string path = System.Web.HttpContext.Current.Server.MapPath("a.txt").ToString();File.WriteAllText(path,context);在write的时候出错了,说找不到文件。楼上的知道是怎么原因吗? 在多线程里面使用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); } } winner2050的方法在discuz.net里用的,但是不知道怎么模拟HttpContext 在ASP.NET中实现文字不间断向左滚动效果? mvc 怎么去掉默认值 IO生成THML, 格式的问题.你可能没碰到过 gridview中的数据更新不了?? 请教高手:关于IE“后退”的问题? 无法连接数据库(急 在线等) 怎么解决这种错误???? 高分求购用vs.net编译C#类的办法 ASP.NET中有关控件的布局问题,在线等,急 在DataGrid里插入下拉框 请教各位:我在gridview中增加了一列textbox,为什么我在后台就找不到这个textbox的id号呢? gmail登录 正在加载 实现
可是现在是在自定义类里的
System.AppDomain.CurrentDomain.BaseDirectory.ToString() + ConfigurationManager.AppSettings["dbPath"]就可以了我找到办法了,就是上面的
FILE.EXIST(1.TXT)他是BOOL型的 你看下返回值
//此处写你自己想执行的任务
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)没有问题,
string path = System.Web.HttpContext.Current.Server.MapPath("a.txt").ToString();
File.WriteAllText(path,context);
在write的时候出错了,说找不到文件。
楼上的知道是怎么原因吗?
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);
}
}