C#取得程序的根目录以及判断文件是否存在
一:获取根目录的方法
取得控制台应用程序的根目录方法
方法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 这个才是获取的根目录,在写获取数据库路径是应该用这个,其他的都有问题。
转载自:http://my.oschina.net/robortly/blog/685631
二:判断文件及文件夹是否存在的方法
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(".") + );//获取已上传文件的后缀
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这个文件夹
ShowText.Text = "你上传的文件名称是:" + name + "<br>" + "文件大小:" + size + "KB" + "<br>" + "文件类型:" + type + "<br>" + "存放的实际路径为:" + fpath; } } }
}
判断文件(夹)是否存在
转载自:http://www.cnblogs.com/CookBlack/archive/2011/04/10/1883864.html
C#取得程序的根目录以及判断文件是否存在的更多相关文章
- C#取得控制台应用程序的根目录方法 判断文件夹是否存在,不存在就创建
取得控制台应用程序的根目录方法1:Environment.CurrentDirectory 取得或设置当前工作目录的完整限定路径2:AppDomain.CurrentDomain.BaseDirect ...
- php判断文件存在是用file_exists 还是 is_file
From: http://www.php100.com/html/php/hanshu/2013/0905/4672.html [导读] 在写程序时发现在判断文件是否存在时,有两种写法,有的人用了is ...
- Linux程序开发中如何判断目录是否为根目录?
问题引入 判断某个目录字符串是否是根目录,咋一听很简单,只要判断字符串是否是"/"即可,但是,很多情况下使用的路径是相对路径,那么如何判断相对路径是根目录呢? 思路分析 熟悉Lin ...
- linux下使用c判断文件夹是否为空的小程序
自己写了一个 判断文件夹是否为空的小代码 //文件夹操作相关的函数的帮助$: man 3 readdir #include <stdio.h> #include <sys/types ...
- python os 命令,及判断文件夹是否存在
使用前 import os导入模块 os模块: os.sep 可以取代操作系统特定的路径分割符 os.linesep 字符串给出当前平台使用的行终止符.例如,Windows使用'\r\n ...
- vc++ 判断文件或是文件夹是否存在,比较好的做法
#include <windows.h> void main() { //文件或文件夹都可以判断,最后的\\号有无都没关系 !=GetFileAttributes("D:\\My ...
- 使用java 程序创建格式为utf-8文件的方法(写入和读取json文件)
使用java 程序创建格式为utf-8文件的方法: try{ File file=new File("C:/11.jsp"); ...
- Ubuntu根目录下各文件夹的功能详细介绍
Ubuntu的根目录下存在着很多的文件夹,但你知道他们都存放着哪些文件呢?这些是深入了解Ubuntu系统必不缺少的知识,本文就关于此做一下介绍吧. /bin/ 用以存储二进制可执行命令文件. / ...
- 使用c#检测文件正在被那个进程占用 判断文件是否被占用的两种方法
C# 判断文件是否被占用的三种方法 using System.IO; using System.Runtime.InteropServices; [DllImport("kernel32.d ...
随机推荐
- Linux下管理员强行踢出用户的命令使用方法
Linux强制踢出用户命令: 一.输入w命令查看已登录用户信息 [root@KW_S01_192.168.1.106_A ~]# w 19:22:31 up 2:11, 3 users, loa ...
- win10系统遇到的问题解决
1.win10 计算器提示:需要新应用打开此calculator 运行calc,会出现需要新应用打开此Calculator,打开应用商店,找到计算器,仍然可以被使用,我怀疑是我自己在清理PC的注册 ...
- Java打包多文件成zip
package com.zh.java.util; import lombok.extern.slf4j.Slf4j; import java.io.File;import java.io.FileI ...
- spark(2.1) - spark-shell 下文件系统的数据读写
spark-shell 本地文件系统数据读写 [ file:// ] 读取 :sc.textFile (" ****") 写入:saveAsTextFile ("**** ...
- Git入门操作
仅学习Git的一些入门操作比较容易,平时更多地使用GitHub,不过今天我想自个搭个服务练练手.当看完一些材料合作了一些验证之后,才发现其实所谓的服务和之前的svn完全不一样了.过程记录如下: Lin ...
- Python 利用循环画散点图
import pandas as pd data = pd.read_csv('D:/suning/iris.csv') data = data.iloc[:,1:] ###2维散点图 import ...
- mysql死锁问题解决
1. 定位问题 http://blog.csdn.net/beiigang/article/details/43228361 2. 解决死锁 http://www.blogbus.com/ri0day ...
- ARC102D(构造)
ARC102D(构造) 构造一个图,使得\(n \le 20,m\le 60\),边从小的点连向大的点,并且从1到n的所有路径,长度与\([0, l-1]\)构成双射. 用二进制的思想--代码很鬼畜 ...
- nginx的worker_processes优化
nginx的worker_processes参数来源: http://bbs.linuxtone.org/thread-1062-1-1.html分享一:搜索到原作者的话:As a general r ...
- AP_MergeSql
) FROM DELTA.PRMCN WHERE ETL_FLAG IN ('A','D')) S; --重跑:删除已跑入数据 DELETE FROM CCRD.PRMCN WHERE JOB_SEQ ...