Reputation: 1569
I need to replace the end of a pathfile in VB. So I try this code :
Private Function getfiledata(ByVal fichier As String) As String
Dim fileReader As String
Dim FichierFinal As String
MsgBox(fichier)
FichierFinal = fichier.Replace("E002.pfx","_E002.pem")
FichierFinal = fichier.Replace("X002.pfx","_X002.pem")
FichierFinal = fichier.Replace("A005.pfx","_A005.pem")
MsgBox(FichierFinal)
fileReader = My.Computer.FileSystem.ReadAllText(FichierFinal)
Return fileReader
End Function
The first MsgBox function return me the following result :
C:/Users/Bruno/Documents/Visual Studio 2010/Projects/SerEbics/SerEbics/bin/Debug/Certificats/512250X002.pfx
But the second return me the same path :
C:/Users/Bruno/Documents/Visual Studio2010/Projects/SerEbics/SerEbics/bin/Debug/Certificats/512250X002.pfx
So I need this result :
C:/Users/Bruno/Documents/Visual Studio2010/Projects/SerEbics/SerEbics/bin/Debug/Certificats/512250_X002.pem
Thank you in advance !
Thomas
Upvotes: 1
Views: 475
Reputation: 563
You are not doing what you think. At each new line you cancelled the previous one
and defining FichierFinal
to a new value.
This will works (plus no need to do this in 3 lines):
FichierFinal = fichier.Replace("E002.pfx","_E002.pem").Replace("X002.pfx","_X002.pem").Replace("A005.pfx","_A005.pem")
Upvotes: 1
Reputation: 724
Try this code u wrong replace:
Private Function getfiledata(ByVal fichier As String) As String
Dim fileReader As String
Dim FichierFinal As String
MsgBox(fichier)
FichierFinal = fichier.Replace("E002.pfx","_E002.pem")
FichierFinal = FichierFinal.Replace("X002.pfx","_X002.pem")
FichierFinal = FichierFinal.Replace("A005.pfx","_A005.pem")
MsgBox(FichierFinal)
fileReader = My.Computer.FileSystem.ReadAllText(FichierFinal)
Return fileReader
End Function
Upvotes: 0
Reputation: 216293
This happens because your last Replace restore the original name in the variable FichierFinal
. You should execute your replaces only if the file ends with one of the expected strings.
If fichier.EndsWith("E002.pfx") Then
FichierFinal = fichier.Replace("E002.pfx","_E002.pem")
Else if fichier.EndsWith("X002.pfx") Then
FichierFinal = fichier.Replace("X002.pfx","_X002.pem")
Else if fichier.EndsWith("A005.pfx") Then
FichierFinal = fichier.Replace("A005.pfx","_A005.pem")
End If
In this way you execute the Replace just one time and not three times. Remember that every time you call Replace a new string is allocated and returned and depending on the context of your calls this could have effects on the performances of your code.
Upvotes: 1
Reputation: 2315
The three lines in a row using teh Replace function are not doing quite what you think. Each time, they are setting FichierFinal
to something new. So they are not building on each other. Try replacing them with this:
FichierFinal = fichier.Replace("E002.pfx","_E002.pem")
FichierFinal = FichierFinal.Replace("X002.pfx","_X002.pem")
FichierFinal = FichierFinal.Replace("A005.pfx","_A005.pem")
Upvotes: 3