?
Solved

Can't translate program from VB to C#

Posted on 2012-12-26
11
Medium Priority
?
856 Views
Last Modified: 2012-12-26
I have a Visual Studio VB.NET program with one form. The form has one text box and one button. You enter text in the text box, click the button, and your input is saved in a new text filed called TextFile1.txt. It works fine.  Here's the code for the Button1_Click.

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles     Button1.Click
        My.Computer.FileSystem.WriteAllText _
            ("c:\TextFile1.txt", TextBox1.Text, False)
        MsgBox("File saved as TextFile1.txt")

 End Sub


I tried to create the same program in Visual C#, but it doesn't work. Here's the code for the Button1_Click.

        private void Button1_Click(object sender, EventArgs e)
        {  
            System.IO.File.WriteAllLines
                (@"C:\TextFile2.txt", TextBox1.Text);
            Microsoft.VisualBasic.Interaction.MsgBox
                ("File saved as TextFile2.txt");
        }

I don't know how to make the C# version work. I'm going to attach both programs
to http://www.ee-stuff.com/Expert/Upload/upload.php as zip files because this page won't let me attach them.
0
Comment
Question by:MBarongan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 34

Expert Comment

by:Paul MacDonald
ID: 38721636
"...but it doesn't work."

That's pretty vague.  What happens that shouldn't, or doesn't that should?
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 38721645
How about
private void Button1_Click(System.Object sender, System.EventArgs e)
{
	My.Computer.FileSystem.WriteAllText("c:\\TextFile1.txt", TextBox1.Text, false);
	Interaction.MsgBox("File saved as TextFile1.txt");

}

Open in new window

Converter
0
 

Author Comment

by:MBarongan
ID: 38721662
I get a dialog box that says "There were build errors. Would you like to continue and run the last  successful build?"  

I click No to close the box, and then these errors appear at the bottom of the IDE.

Error1 on line 22 says "The best overloaded method match for 'System.IO.File.WriteAllLines(string, string[])'
has some invalid arguments"      
        
Error 2 on LIne 22 says"Arugment2: cannot convert 'string to 'string[]'
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

Author Comment

by:MBarongan
ID: 38721670
When I tried
            My.Computer.FileSystem.WriteAllText("c:\\TextFile1.txt", TextBox1.Text, false);
            Microsoft.VisualBasic.Interaction.MsgBox("File saved as TextFile1.txt");

I got this error
"The name 'My' does not exist in the current context"
0
 

Author Comment

by:MBarongan
ID: 38721676
Both versions of the program are on http://www.ee-stuff.com/Expert/Upload/upload.php under Question 27977913.
0
 
LVL 34

Expert Comment

by:Paul MacDonald
ID: 38721684
Try:

private void Button1_Click(object sender, EventArgs e)
        {  
            System.IO.File.WriteAllLines(@"C:\TextFile2.txt", TextBox1.Text.ToString, Encoding.UTF8);
            Microsoft.VisualBasic.Interaction.MsgBox("File saved as TextFile2.txt");
        }
0
 

Author Comment

by:MBarongan
ID: 38721715
When I tried

private void Button1_Click(object sender, EventArgs e)
    {  
      System.IO.File.WriteAllLines(@"C:\TextFile2.txt", TextBox1.Text.ToString, Encoding.UTF8);
       Microsoft.VisualBasic.Interaction.MsgBox("File saved as TextFile2.txt");
     }

I got these two errors again

Error1  "The best overloaded method match for 'System.IO.File.WriteAllLines(string, string[])'has some invalid arguments"      
       
Error 2 "Arugment2: cannot convert 'string to 'string[]'
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 38721787
private void Button1_Click(object sender, EventArgs e)
    {  
      System.IO.File.WriteAllLines(@"C:\TextFile2.txt", TextBox1.Text.ToArray, Encoding.UTF8);
       Microsoft.VisualBasic.Interaction.MsgBox("File saved as TextFile2.txt");
     }

Open in new window

0
 
LVL 34

Expert Comment

by:Paul MacDonald
ID: 38721802
I see.  Try:

private void Button1_Click(object sender, EventArgs e)
    {  
      arrText = (string[])TextBox1.Text;"
      System.IO.File.WriteAllLines(@"C:\TextFile2.txt", arrText, Encoding.UTF8);
       Microsoft.VisualBasic.Interaction.MsgBox("File saved as TextFile2.txt");
     }


It looks like WriteAllLines is looking for an array as its second parameter.  I'm not a C# guy, so I may have flubbed the cast as arrText.  If so, I'm sure you can fix it.
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 38721838
Use WriteAllText(), not WriteAllLines():
        private void Button1_Click(object sender, EventArgs e)
        {  
            System.IO.File.WriteAllText(@"C:\TextFile2.txt", TextBox1.Text);
            MessageBox.Show("File saved as TextFile2.txt");
        }

Open in new window

0
 

Author Closing Comment

by:MBarongan
ID: 38721861
WriteAllText() did the trick. Thank you!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question