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

**URL:** <https://forum.kirupa.com/t/from-vb-net-to-c-net-i-need-a-translator/259816>\
**Category:** Uncategorized\
**Created:** [May 8, 2008, 10:35pm UTC](https://forum.kirupa.com/t/from-vb-net-to-c-net-i-need-a-translator/259816 "2008-05-08T22:35:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kanaeda](https://avatars.discourse-cdn.com/v4/letter/k/9e8a1a/32.png) [@kanaeda](https://forum.kirupa.com/u/kanaeda)\
**Post date:** [May 8, 2008, 10:35pm UTC](https://forum.kirupa.com/t/from-vb-net-to-c-net-i-need-a-translator/259816/1 "2008-05-08T22:35:01Z")

</div>

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](http://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\*;

```
        if (hpf.ContentLength &gt; 0) {

           hpf.SaveAs(Server.MapPath("MyFiles") + "\\" + Path.GetFileName(hpf.FileName));
        }
    }
}

```

I’ve added “using [System.IO](http://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?

---

<div class="post-metadata">

**Author:** ![Templarian](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/templarian/32/7152_2.png) [@Templarian](https://forum.kirupa.com/u/Templarian)\
**Post date:** [May 8, 2008, 11:23pm UTC](https://forum.kirupa.com/t/from-vb-net-to-c-net-i-need-a-translator/259816/2 "2008-05-08T23:23:24Z")

</div>

[http://labs.developerfusion.co.uk/convert/vb-to-csharp.aspx](http://labs.developerfusion.co.uk/convert/vb-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.

```auto
public void Page_Load(object sender, [EventArgs](http://labs.developerfusion.co.uk/SourceViewer/search/SSCLI/EventArgs/) e)
{
  try {
  [HttpFileCollection](http://labs.developerfusion.co.uk/SourceViewer/search/SSCLI/HttpFileCollection/) hfc = Request.Files;
  for (int i = 0; i <= hfc.Count - 1; i++) {
    [HttpPostedFile](http://labs.developerfusion.co.uk/SourceViewer/search/SSCLI/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](http://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.

[whisper]//edit… also this is suppose to be in server-side this section is for non-web based programming[/whisper]

---

<div class="post-metadata">

**Author:** ![GregoryPankov](https://avatars.discourse-cdn.com/v4/letter/g/bbe5ce/32.png) [@GregoryPankov](https://forum.kirupa.com/u/GregoryPankov)\
**Post date:** [May 12, 2008, 12:08pm UTC](https://forum.kirupa.com/t/from-vb-net-to-c-net-i-need-a-translator/259816/3 "2008-05-12T12:08:25Z")

</div>

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.
