1
votes

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

4
Your second Replace makes the change you want, but the third one overwrites the result of the second. I think you intended the second and third Replace statements to be FichierFinal = FichierFinal.Replace("X002.pfx","_X002.pem") and FichierFinal = FichierFinal.Replace("A005.pfx","_A005.pem"). - Blackwood

4 Answers

3
votes

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")
1
votes

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")
1
votes

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.

0
votes

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