C#, VB Get Server MapPath
01 ธันวาคม 2552
วันนี้ขอนำเสนอการเขียนโปรแกรมในเรื่องของการ Get Server MapPath ของ Web Application และ Web Service ที่เราใช้กันอยู่นะครับเพื่อที่จะไม่เป็นปัญหาในการติดตั้ง Web Application หรือ Web Service ที่เราได้ทำการพัฒนาขึ้นมา
ตัวอย่าง Code Get Server MapPath ค่าที่ได้เป็นชนิด String โดย Path ที่ได้เป็น Directory ที่อ้างอิงถึง File ของเราที่เก็บอยู่โดยไม่ได้อ้างอิงหรือระบุถึง File ใดโดยตรง
Source Code C#
//ประกาศ Namespace
//using System.Web.Services;
stringPath = string.Empty;
string stringPath = this.MapPath("");
string stringPath = string.Empty;
stringPath = this.MapPath("stringFileName");
WebService objService = new WebService();
string stringPath = objService.Server.MapPath("");
string stringPath = objService.Server.MapPath("stringFileName");
Source Code VB
'ประกาศ Namespace
'Imports System.Web.Services
Dim stringPath As String = stringPath = Me.MapPath("")
Dim stringPath As String = Me.MapPath("stringFileName")
Dim objService As New WebService
Dim stringPath = objService.Server.MapPath("")
Dim stringPath As String = objService.Server.MapPath("stringFileName")
การที่จะอ้างอิงหรือระบุถึง File ที่เราต้องการนั้นเราก็แค่ทำการต่อ String Dirctory ที่ได้ตามด้วย ชื่อของ File ที่้ต้องการ หรือใช้ Function MapPath ดังตัวอย่าง
Source Code C#
//ประกาศ Namespace
//using System.Web.Services;
string stringPath = string.Empty;
stringPath = this.MapPath("") + @"\FileName.xxx";
string stringPath = this.MapPath("") + @"\FolderName\FileName.xxx";
string stringPath = this.MapPath(@"\FileName.xxx");
string stringPath = this.MapPath(@"\FolderName\FileName.xxx"");
WebService objService = new WebService();
string stringPath = objService.Server.MapPath(@"FileName.xxx");
string stringPath = objService.Server.MapPath(@"FolderName\FileName.xxx");
Source Code VB
'ประกาศ Namespace
'Imports System.Web.Services
Dim stringPath As String = Me.MapPath("") & "\FileName.xxx"
Dim stringPath As String = Me.MapPath("") & "\FolderName\FileName.xxx"
Dim stringPath As String = String.Empty
stringPath = Me.MapPath("FileName.xxx")
Dim stringPath As String = Me.MapPath("FolderName\FileName.xxx")
Dim objService As New WebService
Dim stringPath As String = objService.Server.MapPath("FileName.xxx"")
Dim stringPath As String = objService.Server.MapPath("FolderName\FileName.xxx")
ผิดพลาดประการใดก็ติชมแก้ไขกันนะครับ
ขอบคุณครับ

0 ความคิดเห็น:
แสดงความคิดเห็น