استكشاف الملفات و المجلدات في ASP.net
هذا مثال للتعامل مع الملفات و المجلدات في ASP.net الفكرة و الكود الاساسي ماخوذة من مقال في MSDN وهي :
Walkthrough: Interacting with Files and Folders in Visual Basic .NET
و عنوانها
http://msdn.microsoft.com/library/default....OWhitepaper.asp
و المقال عن فيجول بيسك وتم تحويله الي كود ASP.net
و هذا التطبيق الهدف منه اكستكشاف الملفات الموجودة داخل مجلد معين و عرض بعض المعلومات عن ملف اي ملف داخل هذا المجلد وهذة المعلومات هي خصائص الملف و وقت اخر دخول للملف و حجم الملف و اول سطر( اول 80 حرف) في هذا الملف و امكانية حفظ هذة المعلومات الي ملف خارجي وكما تظهر صور التطبيق بلاسفل:

فيتكون هذا التطبيق من الادوات التالية:
اداة TextBox
لعرض المجلد الحالي
اداة Button
لاعطاء الامر بعرض محتويات المجلد
اداة ListBox
لعرض الملفات الموجودة في الملجلد المختار
اداة Button
لاعطاء الامر بعرض معلومات الملف المختار
عدد 5 اداة CheckBox
تمثل الوظائف الممكنة في هذا التطبيق
و اخيرا اداة Label
لعرض رسائل التطبيق بدلا من استخدام MsgBox
في التطبيق الاصلي باستخدام فيجول بيسك و توجد الاداة في اعلي الصفحة
توضيح بعض الدوال:
CurDir()
و تستخدم لتعود باسم المجلد الحالي
ChDir("أسم الملف")
وتستخدم للانتقال الي مجلد اخر و تاخذ مسار المجلد الجديد كمدخل
GetAttr("أسم الملف")تاخذ اسم الملف كمدخل وتعود برقم يمثل خصائص الملف مثل للقراء فقط
FileDateTime("أسم الملف")
تاخذ اسم الملف كمدخل وتعود بوقت اخر دخول للملف
FileLen("أسم الملف")
تاخذ اسم الملف كمدخل وتعود بوقت اخر دخول للملف
و اليكم كود التطبيق الكامل و هو مرفق ايضا مع الموضوع و يوجد فيه بعض التعليقات يمكن ان تسهل من استيعابه
<%@ Page Language="VB" %>
<%@ import Namespace="System.IO" %>
<script runat="server">
Sub Page_Load(source as Object, e as EventArgs)
If Not Page.isPostback then
txtDirectory.Text = CurDir()
End If
End Sub
Sub btnSubmit_Click(Sender As Object, E As EventArgs)
Dim NewPath As String
Dim ErrorMessage As String
Dim ContentItem As String
' المتغير NewPath يحتوي علي المسلر الجديد الذي ادخله المستخدم
NewPath = txtDirectory.Text
' تغير المكان الي المسار الجديد
Try
ChDir(NewPath)
' التاكدمن ان متغير المسلر ليس خالي
Catch ex As Exception When NewPath = ""
ErrorMessage = "يجب ان تدخل مسار مجلد"
' بيان اذا ما كان ليس مكتوب بصورة صحيحة بدون حرف القرص الصلب
Catch
ErrorMessage = "يجب ان تدخل مسار مجلد صحيح و لاتنسا ان تكتب حرف جزء القرص الصلب الموجود عليه الملف "
Finally
' عرض رسالة الخطا اذا كانت موجودة
If ErrorMessage <> "" Then
lalErrMsg.Text = ErrorMessage
End If
End Try
LstFilePick.Items.Clear
' ".txt" فققط البحث عن الملافات ذات الامتداد
ContentItem = Dir("*.txt")
If ContentItem = "" Then
ErrorMessage = "No textfiles found."
lalErrMsg.Text = ErrorMessage
Else
lalErrMsg.Text = ""
End If
' تكرار الخطوات لاضافة الملفات الموجودة في المجلد الي القائمة
Do Until ContentItem = ""
' الاضافة الي القائمة
lstFilePick.Items.Add(ContentItem)
' التحرك الي الملف التالي
ContentItem = Dir()
Loop
End Sub
Sub btnExamine_Click(Sender As Object, E As EventArgs)
Dim Attributes As FileAttribute
Dim AttributeResult As String
Dim LastAccess As Date
Dim Length As Long
Dim FirstLine As String
Dim FinalString As String
Dim ErrorMessage As String
Dim stringAttributes As String
stringAttributes = "خصائص الملف هي: "
Dim stringLastAccess As String
stringLastAccess = "اخر دخول الي الملف كان في: "
Dim stringLength As String
stringLength = "حجم الملف بالبايت هو: "
Dim strFirstLine As String
strFirstLine = "اول سطر في الملف هو: "
Dim thisFile As String
thisFile = lstFilePick.SelectedItem.text
' فحص اذا ما كان لايوجد ملف مختار
If thisFile Is Nothing Then
lalErrMsg.Text ="لايوجد ملف مختار "
Exit Sub
End If
' فحص خصائص الملف
If chkAttributes.Checked = True Then
Attributes = GetAttr(thisFile)
If Attributes = 2 Then
AttributeResult = "ReadOnly"
ElseIf Attributes = 32 Then
AttributeResult = "Archive"
ElseIf Attributes = 34 Then
AttributeResult = "ReadOnly and Archive"
Else
AttributeResult = "Normal"
End If
' الاضافة الي رسالة ملخص النتائج
FinalString = stringAttributes + AttributeResult + "
"
End If
' فحص اختيار المستخدم عرض اخر دخول الي الملف
If chkLastAccess.checked = True Then
LastAccess = FileDateTime(thisFile)
' الاضافة الي رسالة ملخص النتائج
FinalString = FinalString + stringLastAccess + LastAccess + "
"
End If
' فحص اختيار المستخدم عرض حجم الملف
If chkFileLength.checked = True Then
Length = FileLen(thisFile)
' الاضافة الي رسالة ملخص النتائج
FinalString = FinalString + stringLength + CStr(Length) + "
"
End If
' فحص اختيار المستخدم عرض اول سطر
If chkFirstLine.Checked = True Then
Try
FileOpen(1, thisFile, OpenMode.Input)
FirstLine = (InputString(1, 80))
' الاضافة الي رسالة ملخص النتائج
FinalString = FinalString + strFirstLine + Firstline + "
"
Catch ex As Exception
ErrorMessage = "يوجد شيء خطا في الملف المراد فحصه تاكد من انه ملف نصوص و ليس خاليا"
lalErrMsg.Text = ErrorMessage
Exit Sub
End Try
FileClose(1)
End If
'عرض رسالة النتائج النهائية
If FinalString = "" Then
lalErrMsg.Text = "لم تقم باختيارات"
Else
lalErrMsg.Text = FinalString
End If
' فحص اختيار المستخدم حفظ النتائج
If chkSave.Checked = True Then
' طريقة FileOpen سوف تنشيء الملف ان لم يكن موجود
FileOpen(1, "log.txt", OpenMode.Append)
Writeline (1, finalString)
FileClose(1)
End If
End Sub
</script>
<html dir=rtl>
<head>
</head>
<body>
<form runat="server">
<p>
<asp:Label id="lalErrMsg" runat="server" ForeColor="OrangeRed" Font-Bold="True" BackColor="#80FFFF"></asp:Label>
</p>
<p>
<asp:TextBox id="txtDirectory" runat="server">Directory</asp:TextBox>
<asp:Button id="btnSubmit" onclick="btnSubmit_Click" runat="server" Text="عرض محتويات المجلد"></asp:Button>
</p>
<p>
<asp:ListBox id="lstFilePick" runat="server"></asp:ListBox>
<asp:Button id="btnExamine" onclick="btnExamine_Click" runat="server" Text="فحص الملف"></asp:Button>
</p>
<div align=center><font color=red size=4>أختيارات</font></div>
<p>
<asp:CheckBox id="chkAttributes" runat="server" Text=" عرض خصائص الملف" Checked="True"></asp:CheckBox>
</p>
<p>
<asp:CheckBox id="chkFileLength" runat="server" Text=" عرض حجم الملف" Checked="True"></asp:CheckBox>
</p>
<p>
<asp:CheckBox id="chkLastAccess" runat="server" Text=" عرض اخر دخول للملف" Checked="True"></asp:CheckBox>
</p>
<p>
<asp:CheckBox id="chkFirstLine" runat="server" Text="عرض اول سطرفي الملف" Checked="True"></asp:CheckBox>
</p>
<p>
<asp:CheckBox id="chkSave" runat="server" Text="حفظ النتائج"></asp:CheckBox>
</p>
</form>
</body>
</html>