The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.

Results 1 to 3 of 3

Thread: From VB.net to C#.net ... I need a translator...

  1. #1
    70
    posts
    Unregistered User

    From VB.net to C#.net ... I need a translator...

    Okay, so I've got this code (in VB, which I don't really know) that gets files from a flash file reference list and uploads them:

    <%@ Import Namespace="System.IO" %>

    <script language="vbscript" runat="server">
    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    Try
    Dim hfc As HttpFileCollection = Request.Files
    For i As Integer = 0 To hfc.Count - 1
    Dim hpf As HttpPostedFile = hfc(i)
    If hpf.ContentLength > 0 Then
    'Save the file to some mapped location i.e. "MyFiles"
    hpf.SaveAs(Server.MapPath("MyFiles") & "\" & Path.GetFileName(hpf.FileName))
    End If
    Next i

    Catch ex As Exception
    'some backend error handle
    End Try
    'Response.Write("Hello")

    End Sub
    This works perfectly... I don't know how to use VB properly tho and there's other features I wanna add, so I'm trying to convert this snippet to C#, where am I going wrong?:

    protected void Page_Load(object sender, EventArgs e)
    {
    HttpFileCollection hfc = Request.Files;
    int i;
    for (i = 0; i<hfc.Count-1; i++) {
    HttpPostedFile hpf = hfc[i];

    if (hpf.ContentLength > 0) {

    hpf.SaveAs(Server.MapPath("MyFiles") + "\\" + Path.GetFileName(hpf.FileName));
    }
    }
    }
    I've added "using System.IO" at the top (I normally prefer to keep my code in a seperate to the aspx page)... But this doesn't work... Why?

  2. #2
    http://labs.developerfusion.co.uk/co...to-csharp.aspx

    Its what the converter is made for works 80% of the time. Then the rest has to be done by hand which is usually minor things. (its made for windows development, so You will have to put in the using part and it should work.

    Code:
    public void Page_Load(object sender, EventArgs e)
    {
      try {
      HttpFileCollection hfc = Request.Files;
      for (int i = 0; i <= hfc.Count - 1; i++) {
        HttpPostedFile hpf = hfc(i);
        if (hpf.ContentLength > 0) {
          //Save the file to some mapped location i.e. "MyFiles"
          hpf.SaveAs(Server.MapPath("MyFiles") + "\\" + Path.GetFileName(hpf.FileName));
        }
      }
      }catch (Exception ex) {
      //some backend error handle
      }
      //Response.Write("Hello")
    }
    I can only convert C# to vb.net though by hand I don't know enough C#, probably just wait till one of the many C# users comes by if the online converter doesn't do it for you.

    (//edit... also this is suppose to be in server-side this section is for non-web based programming)
    Last edited by Templarian; May 8th, 2008 at 11:21 PM.

    Join #kirupa | Click the under my Username | CL | Jeff

  3. #3
    Hello!

    Request with files is a first request to mentioned .aspx page or is it post back?

    If it is post back, then you should add if(IsPostBack){} condition for all code inside Page_Load procedure.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012