C# 如何在不同虚拟目录下创建文件夹

用Server.MapPath("相对路径").

  相对路径根据不同情况直接取就可以了:

  // 取当前目录下的test.txt文件

  string path = "test.txt";

  // 取当前目录下的子目录a下的test.txt文件

  string path = "a/test.txt";

  // 取当前目录的平级目录a下的test.txt文件

  string path = "../a/test.txt";

  // 取站点根目录下的子目录a下的test.txt文件

  string path = "~/a/test.txt";

  如果要取绝对路径:

  Server.MapPath(相对路径);

  该方法返回传入的相对路径的绝对路径.

  然后我们再用这个绝对路径来存文件;

  下面是一个上传文件的例子

  首先导入命名空间:

  using System.IO;

  假使页面上有一个FileUpload控件FileUpload1;和一个Button1

  我们在Button1的click事件中这样写:

  private void Button1_Click(Object sender,EventArges e)

  {

  // 取得上传的文件对象

  HttpPostedFile hpf = FileUpload1.PostedFile;

  // 取得文件路径

  string filePath = hpf.FileName;

  // 从路径中取出文件名用来作为保存的文件名

  string filePath = Path.GetFileName(filePath);

  // 取得服务器站点根目录的绝对路径

  string serverPath = Server.MapPath("~/");

  // 保存文件

  hpf.Save(serverPath + filePath);

  }

转:http://www.51testing.com/html/92/n-116292.html
posted @ 2009-04-13 10:47  广陵散仙(www.cnblogs.com/junzhongxu/)  阅读(363)  评论(0编辑  收藏  举报