Saving source code formatting to TXT file - c#

I am trying to extract the source code from a webpage and save it to a text file. However, I want to keep the formatting of the source code.
My code is below.
// this block fetches the source code from the URL entered.
private void buttonFetch_Click(object sender, EventArgs e)
{
using (WebClient webClient = new WebClient())
{
string s = webClient.DownloadString("http://www.ebay.com");
Clipboard.SetText(s, TextDataFormat.Text);
string[] lines = { s };
System.IO.File.WriteAllLines(#"C:\Users\user\Dropbox\Personal Projects\WriteLines.txt", lines);
MessageBox.Show(s.ToString(), "Source code",
MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
}
}
I would like the text file to show the source code as it is formatted in the Messagebox.
Messagebox screenshot:
Text file screenshot:
How would I go about getting the text document's formatting to be the same as in the Messagebox?

I agree with the comment, but I'll add just a note. If you open it in Notepad++, N++ will detect the line endings and display the file nicely for you. In Notepad++ you can go into the menu and change the Line Endings to Windows. If you then re-save it and open it in Notepad itself, it will look correctly. The problem is that the base Notepad doesn't understand different line endings.
Hope it helps.

The problem is that the string you're downloading has LF-only line endings. The Windows standard is CRLF line endings. Windows Notepad is notoriously adamant about supporting only CRLF line endings. Other editors, including Visual Studio, correctly handle the LF-only versions.
You can convert the text to CRLF line endings easily enough:
string s = webClient.DownloadString("http://www.ebay.com");
string fixedString = s.Replace("\n", "\r\n");
System.IO.File.WriteAllText("filename", fixedString);
MessageBox.Show(fixedString, "Source code",
MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
Note also that it is not necessary to call ToString on a string.

Try this:
string[] lines = s.Split('\n');
System.IO.File.WriteAllLines(#"C:\Users\user\Dropbox\Personal Projects\WriteLines.txt", lines);

Related

C# Cant find file (read from serial port)

I´m trying to open a file named the same as a bar code but I get the error that the file cannot be found?
I use this line to read the file:
string[] DxfFile = System.IO.File.ReadAllLines(textBoxBarCodeScanner.Text);
It works fine and open the file correctly if I assign the text box with:
textBoxBarCodeScanner.Text = (#"PLANKA.DXF");
I use these lines to read from the serial port:
RecievedData = RecievedData.Replace("\r\n", "").Replace("\r", "").Replace("\n", "");
textBoxBarCodeScanner.Text = (#RecievedData);
I had to remove /r first but it did not help.
I´m kind a beginner so I´m not so good at finding the right debug information for you so here is what I got and please tell me where I found more useful information.
If I break at the exection and look at "locals" I have a row that says text, and there I got "PLANKA.DFX" which seem to be correct.
Debug error message is as follow:
Additional information: Could not find file E:\Win7\Google
Drive\Visual Studio 2013 Projects\Husmaskin GUI\Husmaskin
GUI\bin\Debug\PLANKA.DFX.
This works (#"PLANKA.DXF"):
This does not (#RecievedData)??
In your screenshots, you have two different values for the file extension: DXF and DFX.
There may be extra whitespace around the incoming text, so I would suggest also adding .Trim() to your code.
I would also suggest checking for the file in your code and creating an error message that is more useful to you:
var filename = txtFoo.Text;
if (!File.Exists(filename))
throw new Exception($"Could not find file '{filename}'");

Open a file without dialog

with openFileDialog you would select a file and after pressing "OPEN" it would paste the filepath of the selected file (c:\blob\template) into an textbox.
I would like to do automatically select the file c:\blob\template en then put the filepath in the textbox. basically the exact same thing as openfiledialog without a dialog. i have been trying to do this for some time now. can somebody help me out with this? i have no clue how to realise this.
i`m only able to get the filepath and paste the string in the textbox but this but only fills the box with a string. i need to load the file/template in there.
private void txt()
{
string fileName = "template";
string fullPath;
fullPath = Path.GetFullPath(fileName);
lblFirstTemplate.Text = fullPath;
}
Thank you in advance!
The code you now have will only get the file path. What you need to add is code that will actually open the file and read its content.
Let's say your file contains some text. You can use the following line to read the complete file as text:
System.IO.File.ReadAllText(fullPath);
If your file contains some other data like binary data, you can use:
System.IO.File.ReadAllBytes(fullPath);
And instead of reading all data at once, you can read it one line or a couple of bytes at a time. A good place in the documentation to start is: Common I/O Tasks

Irregular character/text encoding issue with writing back to file

I'm using this function to read text lines from a file:
string[] postFileLines = System.IO.File.ReadAllLines(pstPathTextBox.Text);
Inserting a few additional lines at strategic spots, then writing the text lines back to a file with:
TextWriter textW = new StreamWriter(filePath);
for (int i = 0; i < linesToWrite.Count; i++)
{
textW.WriteLine(linesToWrite[i]);
}
textW.Close();
This works perfectly well until the text file I am reading in contains an international or special character. When writing back to the file, I don't get the same character - it is a box.
Ex:
Before = W:\Contrat à faire aujourdhui\ `
After = W:\Contrat � faire aujourdhui\ `
This webpage is portraying it as a question mark, but in the text file it's a rect white box.
Is there a way to include the correct encoding in my application to be able to handle these characters? Or, if not, throw a warning saying it was not able to properly write given line?
Add encondig like this:
File.ReadAllLines(path, Encoding.UTF8);
and
new StreamWriter(filePath, Encoding.UTF8);
Hope it helps.
use This , works for me
string txt = System.IO.File.ReadAllText(inpPath, Encoding.GetEncoding("iso-8859-1"));
You can try UTF encoding while writing to the file as well,
textW.WriteLine(linesToWrite[i],Encoding.UTF8);
You may be need to write Single-byte Character Sets
Using Encoding.GetEncodings() you can easily get all possible encoding. ("DOS" encoding are System.Text.SBCSCodePageEncoding)
In your case you may need to use
File.ReadAllLines(path, Encoding.GetEncoding("IBM850"));
and
new StreamWriter(filePath, Encoding.GetEncoding("IBM850"));
Bonne journée! ;)

How to read Cyrillic symbols from a .txt file with C#

I saw similar topics but could not find a solution. My problem is that I have a .txt file in which the symbols are in Bulgarian language / which is Cyrillic /, but after trying to read them, there is no sucess. I tried to read with this code:
StreamReader reader = new StreamReader(fileName,Encoding.UTF8);
if (File.Exists(fileName))
{
while ((line = reader.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
And I also changed the Encoding value to all possible , as I tried with GetEncoding(1251), which I wrote is for cyrillic. And when I save the .txt file I tried to save it with each different encoding which was there / UNICODE,UTF-8,BigEndianUnicode,ANSI / in each combination with the Encoding I am settin through the code, but again no success.
Any ideas for how to read the cyrillic symbols in the right way will be appriciated.
And here is sample text for this: "Ето примерен текст."
Thanks in advance! :)
Your problem is that the console can't show cyrillic characters. Try putting a breakpoint on the Console.WriteLine and inspect the line variable. Clearly you'll need to know the correct encoding first! :-)
If you don't trust me, try this: make a console program that does this:
string line = "Ето примерен текст";
Console.WriteLine(line);
return 0;
put a breakpoint on the return 0;, watch the console and watch the line variable.
I'll add that unicode consoles should be one of the "new" things in .NET 4.5
And you can try to read this page: c# unicode string output
The problem you are having is not reading the text, but displaying it.
If your real intention is to display Unicode text in a console window, then you'll have to make a few changes. If however, you will be displaying the text in a WinForms or WPF app for instance, then you will not have problems - they work with Unicode by default.
By default, the console will not handle unicode, or use a font which has unicode glyphs. You need to do the following:
Save your text file as UTF8.
Start a console which is unicode enabled: cmd \u
Change the font to "Lucida Sans Unicode": console window menu -> properties -> font
Change the codepage to Unicode: chcp 65001
Run your app.
Your characters will now be displayed correctly:

Reading HTML file into textbox translates apostrophes and bullets into?

I am using a StreamReader (in C#) to read contents of an HTML file into a textbox. No matter which encoding I use as an uption, all of the apostrophes and bullets get changed into question marks.
Is there another way to read an HTML file that will preserve these characters?
Thanks!
Jerry
Here is the code:
private void button1_Click(object sender, EventArgs e)
{
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.Cancel)
return;
StreamReader sr = new StreamReader(openFileDialog1.FileName);
txtMessage.Text = sr.ReadToEnd();
sr.Close();
}
I have also used the StreamReader with the Encoding parameter (tried every one). The only thing it seems to do is interpret the question marks are regular or reversed (black diamond with white question mark).
If it makes any difference, the files are created in Word by another department and then exported to Filtered HTML.
One last thing: If I open the HTML file in something like Notepad and copy/paste the text into the textbox, then everything looks exactly as it should.
The changes only occur if I try to pull the file in via a reader.
I would try it with new StreamReader(..., Encoding.UTF8); or new StreamReader(..., Encoding.GetEncoding("iso-8859-1")); and if that doesn't work, then I'd go after the person who created the file and stuff needles under their fingernails until they confess what encoding they used to create it.

Categories