MBarongan
asked on
Can't translate program from VB to C#
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.Wri teAllText _
("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.WriteAllLin es
(@"C:\TextFile2.txt", TextBox1.Text);
Microsoft.VisualBasic.Inte raction.Ms gBox
("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.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
My.Computer.FileSystem.Wri
("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.WriteAllLin
(@"C:\TextFile2.txt", TextBox1.Text);
Microsoft.VisualBasic.Inte
("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.
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");
}
Converter
ASKER
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.WriteAllLi nes(string , string[])'
has some invalid arguments"
Error 2 on LIne 22 says"Arugment2: cannot convert 'string to 'string[]'
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.WriteAllLi
has some invalid arguments"
Error 2 on LIne 22 says"Arugment2: cannot convert 'string to 'string[]'
ASKER
When I tried
My.Computer.FileSystem.Wri teAllText( "c:\\TextF ile1.txt", TextBox1.Text, false);
Microsoft.VisualBasic.Inte raction.Ms gBox("File saved as TextFile1.txt");
I got this error
"The name 'My' does not exist in the current context"
My.Computer.FileSystem.Wri
Microsoft.VisualBasic.Inte
I got this error
"The name 'My' does not exist in the current context"
ASKER
Both versions of the program are on http://www.ee-stuff.com/Expert/Upload/upload.php under Question 27977913.
Try:
private void Button1_Click(object sender, EventArgs e)
{
System.IO.File.WriteAllLin es(@"C:\Te xtFile2.tx t", TextBox1.Text.ToString, Encoding.UTF8);
Microsoft.VisualBasic.Inte raction.Ms gBox("File saved as TextFile2.txt");
}
private void Button1_Click(object sender, EventArgs e)
{
System.IO.File.WriteAllLin
Microsoft.VisualBasic.Inte
}
ASKER
When I tried
private void Button1_Click(object sender, EventArgs e)
{
System.IO.File.WriteAllLin es(@"C:\Te xtFile2.tx t", TextBox1.Text.ToString, Encoding.UTF8);
Microsoft.VisualBasic.Inte raction.Ms gBox("File saved as TextFile2.txt");
}
I got these two errors again
Error1 "The best overloaded method match for 'System.IO.File.WriteAllLi nes(string , string[])'has some invalid arguments"
Error 2 "Arugment2: cannot convert 'string to 'string[]'
private void Button1_Click(object sender, EventArgs e)
{
System.IO.File.WriteAllLin
Microsoft.VisualBasic.Inte
}
I got these two errors again
Error1 "The best overloaded method match for 'System.IO.File.WriteAllLi
Error 2 "Arugment2: cannot convert 'string to 'string[]'
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");
}
I see. Try:
private void Button1_Click(object sender, EventArgs e)
{
arrText = (string[])TextBox1.Text;"
System.IO.File.WriteAllLin es(@"C:\Te xtFile2.tx t", arrText, Encoding.UTF8);
Microsoft.VisualBasic.Inte raction.Ms gBox("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.
private void Button1_Click(object sender, EventArgs e)
{
arrText = (string[])TextBox1.Text;"
System.IO.File.WriteAllLin
Microsoft.VisualBasic.Inte
}
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
WriteAllText() did the trick. Thank you!
That's pretty vague. What happens that shouldn't, or doesn't that should?