Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Find file's type
Message
From
27/02/2017 11:17:20
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 5.0
OS:
Windows 10
Database:
MS SQL Server
Miscellaneous
Thread ID:
01648574
Message ID:
01648575
Views:
34
This message has been marked as a message which has helped to the initial question of the thread.
>I am wondering if someone has a routine to figure out file's type based on either its content or its filename. In my routine I have

This is reading binary signature. Here is an example:
		Try

            ' Get it into a stream
            loFile = New FileStream(lcFile, FileMode.Open, FileAccess.Read, FileShare.Read)

            ' If we can work with the file
            If loFile.CanSeek Then

				' Initialization
				lnLength = loFile.Length

				' Number of bytes to read
				lnByteToRead = 2048

				' Maximum number of bytes to read
				If lnLength < 2048 Then
					lnByteToRead = lnLength
				End If

				' Get the byte-order mark, if there is one
				loByte = New Byte(lnByteToRead) {}

				loFile.Read(loByte, 0, lnByteToRead - 1)
			Else
				cMessage = cUnableToReadTheFile
				Clear()
				Return False
			End If

			' Close the file
			loFile.Close()

		Catch loError As Exception

            ' If we log the error
            If lLogError Then

                ' Get the proper definition as per the current scope
                If oProcess Is Nothing Then
					oApp.ErrorSetup(loError)
				Else
					oProcess.ErrorSetup(loError)
				End If

			End If

			cMessage = loError.Message
			Clear()
			Return False
		End Try

		' Initialization
		lcString = Encoding.Default.GetString(loByte)

		' If we have less than four characters
		If lcString.Length < 4 Then
			cMessage = cNotEnoughCharacter
			Clear()
			Return False
		End If

		' If this is a JPG
		If Mid(lcString, 1, 3) = Chr(255) + Chr(216) + Chr(255) Then
			cExtension = "JPG"
			nNoFileType = 6
			Clear()
			Return True
		End If

		' If this is a GIF
		If Mid(lcString, 1, 3) = "GIF" Then
			cExtension = "GIF"
			nNoFileType = 7
			Clear()
			Return True
		End If

		' If this is a EMF
		If Mid(lcString, 42, 3) = "EMF" Then
			cExtension = "EMF"
			nNoFileType = 13
			Clear()
			Return True
		End If

		' If this is a WMF
		If Mid(lcString, 1, 4) = Chr(215) + Chr(205) + Chr(198) + Chr(154) Then
			cExtension = "WMF"
			nNoFileType = 14
			Clear()
			Return True
		End If
Michel Fournier
Level Extreme Inc.
Designer, architect, owner of the Level Extreme Platform
Subscribe to the site at https://www.levelextreme.com/Home/DataEntry?Activator=55&NoStore=303
Subscription benefits https://www.levelextreme.com/Home/ViewPage?Activator=7&ID=52
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform