How C # creates folders under different virtual directories

Source: Internet
Author: User
Tags relative root directory

With Server.MapPath ("relative path").

Relative paths can be taken directly from different situations:

Take the Test.txt file in the current directory

String path = "Test.txt";

Take the Test.txt file under sub directory A in the current directory

String path = "A/test.txt";

Take the Test.txt file under peer A of the current directory

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

Take the Test.txt file under subdirectory A in the root directory of the site

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

If you want to take an absolute path:

Server.MapPath (relative path);

This method returns the absolute path of the passed relative path.

Then we use this absolute path to save the file;

Here is an example of uploading a file

First Import namespaces:

Using System.IO;

If there is a FileUpload control FileUpload1 on the page; and a Button1

We wrote this in the Button1 click event:

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);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.