19-04-25, 09:36 PM
- Process.start("D\all\mn.docx")استخدم هذا الكود لفتح كتاب وورد اسمه mn موجود على قرص d ضمن مجلد اسمه allكيف بقدر بحال لم يجد المجلد all على القرص d ياتي به من الفلاشة hيعني بحال لم يجده في d ياتي به من h
Imports System.IO
Imports System.Diagnostics
Sub OpenWordDocument()
Dim filePathD As String = "D:\all\mn.docx"
Dim filePathH As String = "H:\all\mn.docx"
If File.Exists(filePathD) Then
Process.Start(filePathD)
ElseIf File.Exists(filePathH) Then
Process.Start(filePathH)
Else
MessageBox.Show("الملف غير موجود على القرص D أو الفلاشة H", "خطأ", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign)
End If
End Sub
Imports System.IO
Imports System.Diagnostics
Sub OpenWordDocument()
For Each drive As DriveInfo In DriveInfo.GetDrives()
If drive.IsReady AndAlso (drive.DriveType = DriveType.Removable OrElse drive.DriveType = DriveType.Fixed) Then
Dim path As String = Path.Combine(drive.Name, "all\mn.docx")
If File.Exists(path) Then
Process.Start(path)
Exit Sub
End If
End If
Next
MessageBox.Show("الملف mn.docx غير موجود في أي قرص.", "خطأ", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign)
End Sub
OpenWordDocument()
Imports System.IO
Public Class Form1
Private Sub btnOpenDoc_Click(sender As Object, e As EventArgs) Handles btnOpenDoc.Click
Dim fileName As String = TxtFileName.Text.Trim()
WordFileOpener.OpenWordDocument(fileName)
End Sub
End Class
Imports System.IO
Imports System.Diagnostics
Public Class WordFileOpener
Public Shared Sub OpenWordDocument(fileName As String)
If String.IsNullOrWhiteSpace(fileName) Then
MessageBox.Show("يرجى إدخال اسم الملف.", "تنبيه", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign)
Return
End If
' تأكد من أن الاسم ينتهي بـ .docx
If Not fileName.ToLower().EndsWith(".docx") Then
fileName &= ".docx"
End If
For Each drive As DriveInfo In DriveInfo.GetDrives()
If drive.IsReady AndAlso (drive.DriveType = DriveType.Removable OrElse drive.DriveType = DriveType.Fixed) Then
Dim path As String = IO.Path.Combine(drive.Name, "all", fileName)
If File.Exists(path) Then
Process.Start(path)
Exit Sub
End If
End If
Next
MessageBox.Show("الملف " & fileName & " غير موجود في أي قرص.", "خطأ", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign)
End Sub
End Class
Imports System.IO
Imports System.Diagnostics
Public Class FileOpener
Public Shared Sub OpenDocument(fileName As String, fileExtension As String)
If String.IsNullOrWhiteSpace(fileName) OrElse String.IsNullOrWhiteSpace(fileExtension) Then
MessageBox.Show("يرجى إدخال اسم الملف ونوع الامتداد.", "تنبيه", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign)
Return
End If
' تأكد من وجود النقطة في الامتداد
If Not fileExtension.StartsWith(".") Then
fileExtension = "." & fileExtension
End If
Dim fullFileName As String = fileName & fileExtension
For Each drive As DriveInfo In DriveInfo.GetDrives()
If drive.IsReady AndAlso (drive.DriveType = DriveType.Removable OrElse drive.DriveType = DriveType.Fixed) Then
Dim path As String = IO.Path.Combine(drive.Name, "all", fullFileName)
If File.Exists(path) Then
Process.Start(path)
Exit Sub
End If
End If
Next
MessageBox.Show("الملف " & fullFileName & " غير موجود في أي قرص.", "خطأ", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign)
End Sub
End Class
Private Sub BtnOpenFile_Click(sender As Object, e As EventArgs) Handles BtnOpenFile.Click
Dim fileName As String = TxtFileName.Text.Trim()
Dim extension As String = TxtExtension.Text.Trim()
FileOpener.OpenDocument(fileName, extension)
End Sub
Imports System.IO
Imports System.Diagnostics
Imports System.Windows.Forms
Public Class FileOpener
' فتح ملف بالاسم والامتداد من مجلد all في جميع الأقراص
Public Shared Sub OpenDocument(fileName As String, fileExtension As String)
If String.IsNullOrWhiteSpace(fileName) OrElse String.IsNullOrWhiteSpace(fileExtension) Then
MessageBox.Show("يرجى إدخال اسم الملف ونوع الامتداد.", "تنبيه", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign)
Return
End If
If Not fileExtension.StartsWith(".") Then
fileExtension = "." & fileExtension
End If
Dim fullFileName As String = fileName & fileExtension
For Each drive As DriveInfo In DriveInfo.GetDrives()
If drive.IsReady AndAlso (drive.DriveType = DriveType.Removable OrElse drive.DriveType = DriveType.Fixed) Then
Dim path As String = IO.Path.Combine(drive.Name, "all", fullFileName)
If File.Exists(path) Then
Process.Start(path)
Exit Sub
End If
End If
Next
MessageBox.Show("الملف " & fullFileName & " غير موجود في أي قرص.", "خطأ", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign)
End Sub
' فتح الملف يدويًا باستخدام OpenFileDialog
Public Shared Sub OpenFileWithDialog()
Dim ofd As New OpenFileDialog()
ofd.Title = "اختر ملفًا"
ofd.Filter = "ملفات Word|*.docx|ملفات PDF|*.pdf|كل الملفات|*.*"
If ofd.ShowDialog() = DialogResult.OK Then
Try
Process.Start(ofd.FileName)
Catch ex As Exception
MessageBox.Show("حدث خطأ أثناء فتح الملف: " & ex.Message, "خطأ", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign)
End Try
End If
End Sub
End Class
Private Sub BtnOpenByName_Click(sender As Object, e As EventArgs) Handles BtnOpenByName.Click
Dim fileName As String = TxtFileName.Text.Trim()
Dim extension As String = TxtExtension.Text.Trim()
FileOpener.OpenDocument(fileName, extension)
End Sub
Private Sub BtnOpenByDialog_Click(sender As Object, e As EventArgs) Handles BtnOpenByDialog.Click
FileOpener.OpenFileWithDialog()
End Sub
Public Class Form1
Sub OpenWordDocument()
Dim filePathD As String = "D:\all\4.docx"
Dim filePathH As String = "H:\all\4.docx"
Dim filePathe As String = "e:\all\4.docx"
Dim filePathc As String = "c:\all\4.docx"
Dim filePathg As String = "g:\all\4.docx"
If File.Exists(filePathD) Then
Process.Start(filePathD)
ElseIf File.Exists(filePathH) Then
Process.Start(filePathH)
ElseIf File.Exists(filePathc) Then
Process.Start(filePathc)
ElseIf File.Exists(filePathe) Then
Process.Start(filePathe)
ElseIf File.Exists(filePathg) Then
Process.Start(filePathg)
Else
MessageBox.Show("الملف المطلوب غير موجود على اي قرص", "خطأ", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign)
End If
End Sub
بقدر اختصر هذا الكود لو سمحتم