• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

C#获得程序的根目录以及判断文件是否存在的实例讲解

c# 搞代码 4年前 (2022-01-09) 40次浏览 已收录 0个评论

一:获取根目录的方法

取得控制台应用程序的根目录方法

方法1、Environment.CurrentDirectory 取得或设置当前工作目录的完整限定路径

方法2、AppDomain.CurrentDomain.BaseDirectory 获取基目录,它由程序集冲突解决程序用来探测程序集

取得WinForm应用程序的根目录方法

1、Environment.CurrentDirectory.ToString();//获取或设置当前工作目录的完全限定路径

2、Application.StartupPath.ToString();//获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称

3、Directory.GetCurrentDirectory();//获取应用程序的当前工作目录

4、AppDomain.CurrentDomain.BaseDirectory;//获取基目录,它由程序集冲突解决程序用来探测程序集

5、AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//获取或设置包含该应用程序的目录的名称

取得web应用程序的根目录方法

1.HttpContext.Current.Server.MapPath(“~/configs/ChannelUsers.xml”)

HttpContext.Current

返回当前请求的 HttpContext 对象。如此我们就可以直接访问Request、Response、Session、Application等对象,和Page中访问等同。

我们无需再将Page用参数的方式传递到我们的类库对象中。

HttpContext.Current.Session["name"] = "猪八戒";
string name = HttpContext.Current.Request.Param["name"];
HttpContext.Current.Response.Write("猪八戒好吃懒做!");

获取网站根目录的方法有几种如:

Server.MapPath(Request.ServerVariables["PATH_INFO"])
Server.MapPath("/")
Server.MapPath("")
Server.MapPath(".")
Server.MapPath("../")
Server.MapPath("..") 
Page.Request.ApplicationPath

运行结果:

C:\Inetpub\wwwroot\EnglishClub\manage\WebForm1.aspx
C:\Inetpub\wwwroot\
C:\Inetpub\wwwroot\EnglishClub\manage
C:\Inetpub\wwwroot\EnglishClub\manage
C:\Inetpub\wwwroot\EnglishClub\
C:\Inetpub\wwwroot\EnglishClub

以上的方法可以在.aspx中访问,但是如果你在。cs文件就不能用。

HttpContext.Current.Server.MapPath();
System.Web.HttpContext.Current.Request.PhysicalApplicationPath 

在.cs文件中可以用。

但是HttpContext.Current.Server.MapPath();这个获取的是文件的路径而不是根目录。

只有System.Web.HttpContext.Current.Request.PhysicalApplicationPath 这个才是获取的根目录,在写获取数据库路径是应该用这个,其他的都有问题。

二:判断文件及文件夹是否存在的方法

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
 
public partial class Default3 : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
   ShowPic.Visible = false;//初始化不显示
   ShowText.Visible = false;//初始化不显示
  }
  protected void Button1_Click(object sender, EventArgs e)
  {
 
   if (Directory.Exists(Server.MapPath("~/upimg/hufu")) == false)//如果不存在就创建file文件夹
   {
    Directory.CreateDirectory(Server.MapPath("~/upimg/hufu"));
   }
 
   //Directory.Delete(Server.MapPath("~/upimg/hufu"), true);//删除文件夹以及文件夹中的子目录,文件 
 
   //判断文件的存在
 
   if (File.Exists(Server.MapPath("~/upimg/Data.html")))
   {
    Response.Write("Yes");
 
    //存在文件
 
   }
 
   else
   {
    Response.Write("No");
    //不存在文件
    File.Create(MapPath("~/upimg/Data.html"));//创建该文件
 
   }
 
   string name = GetFiles.FileName;//获取已上传文件的名字
   string size = GetFiles.PostedFile.ContentLength.ToString();//获取已上传文件的大小
   string type = GetFiles.PostedFile.ContentType;//获取已上传文件的MIME
   string postfix = name.Substring(name.LastIndexOf(".") + 1);//获取已上传文件的后缀
   string ipath = Server.MapPath("upimg") +"\\"+ name;//获取文件的实际路径
   string fpath = Server.MapPath("upfile") + "\\" + name;
   string dpath = "upimg\\" + name;//判断写入数据库的虚拟路径
 
   ShowPic.Visible = true;//激活
   ShowText.Visible = true;//激活
 
   //判断文件格式
   if (name == "") { 
   Response.Write("<script>alert('上传文件不能为空')</script>");
   }
 
   else{
 
    if (postfix == "jpg" || postfix == "gif" || postfix == "bmp" || postfix == "png")
    {
     GetFiles.SaveAs(ipath);
     ShowPic.ImageUrl = dpath;
     ShowText.Text = "你上传的图片名称是:" + name + "<br>" + "文件大小:" + size + "KB" + "<br>" + "文件类型:" + type + "<br>" + "存放的实际路径为:" + ipath;
 
    }
 
    else
    {
     ShowPic.Visible = false;//隐藏图片
     GetFiles.SaveAs(fpath);//由于不是图片文件,因此转存在upfile这个文件夹
   <em>本文来源[email protected]搞@^&代*@码)网5</em>  ShowText.Text = "你上传的文件名称是:" + name + "<br>" + "文件大小:" + size + "KB" + "<br>" + "文件类型:" + type + "<br>" + "存放的实际路径为:" + fpath;
 
    }
 
   
   }
 
 
  }
}


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:C#获得程序的根目录以及判断文件是否存在的实例讲解

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址