I'm new to C#, and i'm trying to parse a csv file line by line, to search for a string inside each line and do some action.
My problem is that after reading the line, I used Messagebox.Show("Line: " + CurrentLine); just to check if the output is correct, but while the line should show something like C:\Users\test\Desktop\598\Root\test.xls, it just shows up C (the first char).
I used the same code on a console application and the output is fine.
Is there anything wrong with the Messagebox.Show to show the char : ?
Here is my code:
var reader = new StreamReader(File.OpenRead(Filename));
string Search = "test";
string CurrentLine = reader.ReadLine();
MessageBox.Show("Line: " + CurrentLine);
Thanks
Related
I'm having two problems with reading my .csv file with streamreader. What I'm trying to do is get the values, put them into variables which I'll be using later on, inputting the values into a browser via Selenium.
Here's my code (the Console.Writeline at the end is just for debugging):
string[] read;
char[] seperators = { ';' };
StreamReader sr = new StreamReader(#"C:\filename.csv", Encoding.Default, true);
string data = sr.ReadLine();
while((data = sr.ReadLine()) != null)
{
read = data.Split(seperators);
string cpr = read[0];
string ydelsesKode = read[1];
string startDato = read[3];
string stopDato = read[4];
string leverandoer = read[5];
string leverandoerAdd = read[6];
Console.WriteLine(cpr + " " + ydelsesKode + " " + startDato + " " + stopDato + " " + leverandoer + " " + leverandoerAdd);
}
The code in and of itself works just fine - but I have two problems:
The file has values in Danish, which means I get åøæ, but they're showing up as '?' in console. In notepad those characters look fine.
Blank values also show up as '?'. Is there any way I can turn them into a blank space so Selenium won't get "confused"?
Sample output:
1372 1.1 01-10-2013 01-10-2013 Bakkev?nget - dagcenter ?
Bakkev?nget should be Bakkevænget and the final '?' should be blank (or rather, a bank space).
"Fixed" it by going with tab delimited unicode .txt file instead of .csv. For some reason my version of excel doesn't have the option to save in unicode .csv...
Don't quite understand the problem of "rolling my own" parser, but maybe someday someone will take the time to explain it to me better. Still new-ish at this c# stuff...
I've been trying to do a renamer program in c# for 2 different paths and I keep getting error "Path includes invalid characters" I have no clue how to fix it, I've tried adding # and deleting \ and keeping only one . But still didn't figure out how to fix it. Would love any help.
This is what gives me an error:
if (French.Checked)
{
directoryfile = #"C:\Users\" + curruser + #"\Appdata\Local\fo4renamer\directory.txt";
label1.Text = directoryfile;
readpath = File.ReadAllText(directoryfile);
string shouldwork = readpath + "data";
string french = shouldwork + "\\french";
string german = shouldwork + "\\german";
string tmp = shouldwork + "tmp.txt";
label1.Text = french;
string path2 = #"C:\Users\duchacekda\Desktop\e\Renamer\Renamer\bin\Debug\tmp.txt";
string filename = #"C:\Users\duchacekda\Desktop\e\Renamer\Renamer\bin\Debug\french.txt";
File.Move(french, german);
}
Here is the whole code:
https://pastebin.com/0i7fzh24
Edit: this is the string for curruser
string curruser = System.Environment.UserName;
The exception was given by this line
File.Move(french, german);
File.ReadAllText Method (String) : Opens a text file, reads all lines of the file, and then closes the file.
So in your scenario :
string french = (Content of directory.txt) + "data" + "\\french";
It depends on content of directory.txt
a) If content = directory path(c:\foo) there is no problem
b) if content = "dummy text *** dummy text" then it will throw exception
Please check content of file
Found the mistake, I used WriteLine instead of Write so it added enter on the end of the line which made the path incorrect, thanks for the help
I'm trying to replace pipe symbol(|) with new line(\n) in my text(test1.txt) file. But when I'm trying to save it in text(test2.text) file the result is not coming in my test2.txt file but I see the result in my console window. Any one please help on this.
string lines = File.ReadAllText(#"C:\NetProject\Nag Assignments\hi.txt");
//string input = "abcd|efghijk|lmnopqrstuvwxyz";
lines = lines.Replace('|', '\n');
File.WriteAllText(#"C:\NetProject\Nag Assignments\hi2.txt", lines);
Console.WriteLine(lines);
You can try this one:
lines = lines.Replace("|", Environment.NewLine);
It returns "\r\n", for non-Unix platforms according to documentation.
Seems like you want multiple things here. (both original question and subsequent comments)
One is to separate the lines and be able to reference them separately:
string[] separatedLines = lines.Split('|');
The other is to join them back together with a different separator:
string rejoinedLines = string.Join(Environment.NewLine, separatedLines);
You then have access to the individual lines from the separatedLines variable above such as separatedLines[0] and you can also write the rejoinedLines variable back to the other file like you wanted.
EDIT: For example, the following code:
string lines = "a|bc|def";
string[] separatedLines = lines.Split('|');
string rejoinedLines = string.Join(Environment.NewLine, separatedLines);
for (int i = 0; i < separatedLines.Length; i++)
{
Console.WriteLine("Line {0}: {1}", i + 1, separatedLines[i]);
}
Gives output of:
Line 1: a
Line 2: bc
Line 3: def
Instead of:
lines = lines.Replace('|', '\n');
Try:
lines = lines.Replace("|","\r\n");
string[] space = lines.Split ('|');
Will save every substring in space.
The line break should be \r\n for carriage return. It depends if you are reading a file binary or text mode. \n is used in text mode while \r\n is used in binary mode.
Helo to all. I have a text file called mytext.txt and inside the text have a format like this:
<name1>Myname
age
address
postal code
<name2>Myname
age
address
postal code
....(more of the same)
the problem i have is that i must print this text in a richTextBox in format like this:
<name1>Myname
-age
-address
-postal code
<name2>Myname
-age
-address
-postal code
....(more of the same)
any idea how i must do this ?
I won't give you the exact code, but I can give you a pseudo-code representation of what a working algorithm would look like:
function printTextFile()
{
for(every line in the text file)
// So start the loop through each line.
if(current line does not start with "<")
{
prefix:= " -".
// So we're not at a title part.
}
print(prefix + current line).
// Print out the line with the indent.
prefix:= "".
// reset the prefix.
end for loop.
}
var writer = new StringBuilder();
foreach (var line in File.ReadAllLines("mytext.txt"))
{
writer.AppendLine(!line.StartsWith("<") ? string.Format(" -{0}", line) : line);
}
richTextBox1.Text = writer.ToString();
Hi i am doing a small project in C#, and i need to know what commands are comming from input source so i can do my work accordingly..
here is example...
textBox1.Text = "First line \nSecond line";
richTextBox1.Text = "First line \nSecond line";
Richtextbox shows this output:
First line
Second line
Textbox show this output:
First line Second line
please tell me how to show new line "\n" or return "\r" or similar input as a character output in text or richtextbox. so i can know that newline command is coming from input data.
for example text or richtext box will show this output.
First line \nSecond line
thankx in advance.
Lets say that i have a string which have new line :
string st = "my name is DK" + Environment.NewLine + "Also that means it's my name";
Now that i want to show that there is new line in my text there you go :
textBox1.Text = st.Replace(Environment.NewLine, "%");
This will show the newline chat with % sign
For winforms application set
this.textBox1.Multiline = true;
and use "\r\n" as
textBox1.Text = "First line \r\nSecond line";
You want to either prefix your string with # or you can use a double slash before each n (\n). Both of these are ways of escaping the \ so that it displays instead of being treated as part of a new line.
#"This will show verbatim\n";
"This will show verbatim\\n";
You can utilize this by performing a Replace on your incoming text
richTextBox1.Text = richTextBox1.Text.Replace("\n", "\n\\n");
richTextBox1.Text = richTextBox1.Text.Replace("\r\n", "\r\n\\n");
In the replace, I left the original linebreak so that it will be there, just followed by the displaying version. You can take those out if you dont want that. :)
Use the combination "\r\n" (or Environment.NewLine constant which contains just that).
Change Your text box property from single line to Multiline then it will change to new line
TextBox1.TextMode = TextBoxMode.MultiLine;
TextBox1.Text = "First line \n New line ";
TextBoxt1.Text ="FirstLine \r\n SecondLine";