Jump to content

Erro Programa Trial Demo


Go to solution Solved by ribeiro55,

Recommended Posts

Posted

Boa Tarde,

Precisava de uma ajuda num erro que não estou a conseguir resolver.

O programa consiste em um Trial Demo para um programa.

No programa inicial tem o numero de dias na fonte para o programa expirar, agora queria colocar o numero

de dias no ficheiro TXT, para isso fiz as seguintes alterações no seguinte código:

//	

Public Class cTrial

Private m_DaysUsed As Integer = 0
'Private m_MaxDays As Integer = 8 ---> Código original
	Private m_MaxDays As String() = System.IO.File.ReadAllLines( "d:\teste.txt")
Private m_Ended As Boolean = False
Private m_Filename As String = vbNullString

Public ReadOnly Property DaysUsed As Integer
	Get
		'Return number of days used.
		Return m_DaysUsed
	End Get
End Property

Public ReadOnly Property DaysLeft As Integer
	Get
		'Return number of days left.
		Return (MaxDays - m_DaysUsed)
	End Get
End Property

Public Property Filename As String
	Get
		Return m_Filename
	End Get

	Set(ByVal value As String)
		'Set filename.
		m_Filename = value
		'Check if trial file is found.
		If Not File.Exists(Filename) Then
			'Create trial file.
			If Not UpdateFile(Now.Date.AddDays(MaxDays), 0) Then
				Throw New FileNotFoundException
				Exit Property
			End If
		End If

		'Check the date.
		CheckDate(Now.Date)
	End Set
End Property

Public ReadOnly Property HasEnded As Boolean
	Get
		Return m_Ended
	End Get
End Property

Public Property MaxDays As String
	Get
		Return m_MaxDays '---> Erro aqui (Error BC30311 Value of type 'String()' cannot be converted to 'String')

	End Get
	Set(ByVal value As String)
'm_MaxDays = value---> Código original
			m_MaxDays = System.IO.File.ReadAllLines(tdayscont)
	End Set
End Property

Private Sub CheckDate(ByVal Date1 As DateTime)

	Dim sr As StreamReader = Nothing
	Dim Date2 As DateTime = Nothing
	Dim Flag As Integer = 0
	Dim Tmp As String = vbNullString

	Try
		sr = New StreamReader(Filename)
		'Read in file contents.
		Tmp = XorString(sr.ReadToEnd())
		'Close file.
		sr.Close()

		'Read trial flag.
		Flag = Integer.Parse(Tmp(0))
		'Read date.
		Date2 = Tmp.Substring(1)

		'Check if trial finished.
		If (Flag <> 0) Then
			m_DaysUsed = MaxDays
			m_Ended = True
		Else
			'Return number of days used.
			m_DaysUsed = MaxDays - DateDiff("d", Date1, Date2) + 1
			'Check if in range.
			m_Ended = (DaysUsed <= 0) Or (DaysUsed > MaxDays)
		End If
	Catch ex As Exception
		m_DaysUsed = MaxDays
		m_Ended = True
	End Try

	If HasEnded Then
		m_DaysUsed = MaxDays
		'Add date and trial end flag.
		UpdateFile(Date2, 1)
	End If

End Sub

Private Function UpdateFile(ByVal EndDate As DateTime, ByVal Flag As Integer) As Boolean
	Dim sw As StreamWriter = Nothing
	'File Format() Is as follows flag, Date
	'Flag 0 = still in trial
	'Flag 1 = trial has ended.

	Try
		sw = New StreamWriter(Filename)
		'Write date and trial flag.
		sw.Write(XorString(Flag & EndDate.Date))
		'Close file.
		sw.Close()
	Catch ex As Exception
		Return False
	End Try

	Return True
End Function

Private Function XorString(ByVal source As String) As String
	Dim sb As New System.Text.StringBuilder()

	Rnd(-1)
	'Set Random seed.
	Randomize(1830)

	For Each c As Char In source
		'Encrypt bye
		Dim b As Byte = Asc(c) Xor Int(Rnd() * 256) Mod 255
		'Append char to stringbuilder.
		sb.Append(Chr(b))
	Next c
	'Return string
	Return sb.ToString()

End Function
End Class
 

Obrigado

  • Solution
Posted

Usa antes o ReadAllText em vez do ReadAllLines. Este último devolve um array de String e não uma String.

O erro diz basicamente que não pode transformar um array numa string.

Sérgio Ribeiro


"Great coders aren't born. They're compiled and released"
"Expert coders do not need a keyboard. They just throw magnets at the RAM chips"

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.