converting C# to VB problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ahobart
    New Member
    • May 2007
    • 2

    converting C# to VB problem

    Hello, I have this code in C# and it works perfect:
    Code:
    protected string GetUniqueName(string filename) {
    string temp = filename;
    
    for (int x=1; System.IO.File.Exists(temp); x++){
    temp = filename.Substring(0, filename.LastIndexOf(".")) + x + filename.Substring(filename.LastIndexOf("."));
    }
    return new System.IO.FileInfo(temp).Name;
    }
    Then I converted it to VB:
    [code=vbnet]
    Protected Function GetUniqueName(B yVal filename As String) As String
    Dim temp As String = filename
    Dim x As Integer = 1
    While System.IO.File. Exists(temp)
    temp = filename.Substr ing(0, filename.LastIn dexOf(".")) + x + filename.Substr ing(filename.La stIndexOf("."))
    System.Math.Min (System.Threadi ng.Interlocked. Incre ment(x), x - 1)
    End While
    Return (New System.IO.FileI nfo(temp)).Name
    End Function
    [/code]
    Now I am getting an error:

    Input string was not in a correct format.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.FormatEx ception: Input string was not in a correct format.

    Source Error:

    Line 303: Dim x As Integer = 1
    Line 304: While System.IO.File. Exists(temp)
    Line 305: temp = filename.Substr ing(0, filename.LastIn dexOf(".")) + x + filename.Substr ing(filename.La stIndexOf("."))
    Line 306: System.Math.Min (System.Threadi ng.Interlocked. Incre ment(x), x - 1)
    Line 307: End While

    Source File: D:\wwwroot\www.yourkinetico.com\wwwroot\AdPlanner\ App_Code\Utils\ Upload.vb Line: 305

    Stack Trace:

    [FormatException : Input string was not in a correct format.]
    Microsoft.Visua lBasic.Compiler Services.Conver sions .ParseDouble(St ring Value, NumberFormatInf o NumberFormat) +211
    Microsoft.Visua lBasic.Compiler Services.Conver sions .ToDouble(Strin g Value, NumberFormatInf o NumberFormat) +74


    Any help would be greatly appreciated!
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    If someone sends in a file without a . in it, your program will fail.

    The System.IO.File and System.IO.FileI nfo classes exist inside VB, why mess with success?

    TADA: (you needed a .ToString() on that x)
    [code=vbnet]
    Protected Function GetUniqueName(B yVal filename As String) As String
    Dim temp As String = filename
    Dim x As Integer = 1
    While System.IO.File. Exists(temp)
    temp = filename.Substr ing(0, filename.LastIn dexOf(".")) + x.ToString() + filename.Substr ing(filename.La stIndexOf("."))
    x = x + 1
    End While
    Return (New System.IO.FileI nfo(temp)).Name
    End Function
    [/code]

    Comment

    Working...