How to make Console.Clear clear all except one specific line? (c#)
So, as a title says i want to clear the whole console except one specific line using c#. Is it possible?
Well, yes, it's possible. But it's not especially easy. You'll have to become familiar with the Console API, including how to call those functions with .NET. Then, what you can do:
Locate the line you want to save. If you know where it is on the screen, then it's easy to locate in the console screen buffer.
Call FillConsoleOutputCharacter to output spaces from the start of the screen buffer up to the first character of the line that you want to save.
Call FillConsoleOutputCharacter again to output spaces from the end of the line you want to save up to the end of the buffer.
I wrote a series of articles about accessing the console from C# a few years back. The articles are no longer online, but the code is available from http://mischel.com/pubs/consoledotnet.zip. You might find it useful.
yes it is possible. just use this code...
string line = yourlinenottobedeleted;
console.clear();
console.WriteLine(line);
And the deed is done
(if the line that is not to be deleted is not a string, then put int instead of string)
Related
Sorry in advance for the inappropriate tag (this is more a NSDK issue than a C# issue, but NSDK tag wasn't existing, and I couldn't create it so I had to choose one...)
I'm currently writting a webservice using C#.
My goal is to reproduce what a NSDK code is doing.
Everything is going ok so far but one thing.
I have absolutely no clue about what the skip instruction is doing.
Here is an exemple of an instruction
if skip SomeString <> ''
I know this is testing if someString is empty or not, but the "skip" makes me wonder what it does.
The main goal of my webservice is to create a file, and to send it to a printer after a writing phase and I need to be vary careful with spaces or backlines and stuff so if someone could explain this to me, I'd gladly appreciate!
Best Regards.
Got my Answer.
skip instruction is removing spaces at the beginning and the end of the string.
For instance :
VALUE$ = " key "
There is 2 spaces before and 1 space after the value.
length(VALUE$) gives 6 (the 3 spaces + the three letters)
length(skip VALUE$) gives 3 (only the three letters)
So skip VALUE$ equals "key", and not " key "
I'm trying to use the MSWord Interop Library to write a C# application that outputs specially formated text (isolated arabic letters) to a file. The problem I'm running into is determining how many characters remain before the text wraps onto a new line. I need the words to be on the same line, without wrapping, which is the default behavior. I'm finding this difficult because when I have the Arabic letters of the word isolated with spaces, they are treated as individual characters and therefore behave differently then connected words.
Any help is appreciated. Thanks.
Add each character to your range and then check the number of lines in the range
LineCount = range.ComputeStatistics(Word.WdStatistic.wdStatisticLines);
When the line count changes, you know it has been wrapped, and can remove the last character or reformat accordingly
Actually I don't know how this behaves today, but I've written something for the MSWork API when I was facing a somewhat weird fact. Actually you can't find that out. In MSWord, text in a document is always in paragraphs.
If you input text to your document, you won't get it in a page only, but this page will at least contain a paragraph for the text you wrote into it.
Unfortunately I can't figure this out again, because I don't have a license for MS Word these day.
Give it a try and look at the problem again in this way.
Hope this helps, and if not, please provide the code that generates the input and the exact version of MSWord.
Greetings,
Kjellski
I'm not sure what "Arabic letters of the word isolated with spaces" means exactly, but I assume that non breaking space is what you need.
Here's more details.
Here is a sample string I got off a socket stream.
\033[H\033[J\033[1;30HSUPERVISOR MAIN MENU\033[6;5H 0. Exit Exit\033[7;5H 1. Help Display help\033[8;5H 2. Control Calling lists and users\033[9;5H 3. Campaign Campaigns\033[10;5H 4. Manage
If you want to see the output I expect open a unix/linux shell, type echo -e followed by a space followed by the above string in single quotes and hit Enter key. The output appears something like:
SUPERVISOR MAIN MENU
0. Exit Exit
1. Help Display help
2. Control Calling lists and users
3. Campaign Campaigns
4. Manage
I want the same output except it should be in memory...I require to work with it later...any ideas.
I asked an almost equivalent question a few hours ago: Open Source C# VT100 Server. You want a client library that understands the vt100 escape commands.
I searched around for a while and to date haven't found any very good vt100 C# libraries. I've gotten started on a custom one and since I really only need to interpret left and right arrows and backspace it hasn't taken long.
Luckily the vt100 standard is very promiscuous and not overly complex. I don't think it would take you very long to whip up some code to understand the escape commands in your example. This link has a nice concise list of the VT100 escape sequences (you need to scroll down a bit). Another good site is vt100.net.
In your example the escape sequences are being in octal. Your first escape sequence is:
\033[H
which translates to the ASCII below and is used to set the cursor position.
ESC [ H
The second one is
\033[J
which translates to the ASCII sequence below and means clear the line to the end of screen.
ESC [ J
Hello I am working on something, and I need to be able to be able to add text into a .txt file. Although I have this completed I have a small problem. I need to write the string in the middle of the file more or less. Example:
Hello my name is Brandon,
I hope someone can help, //I want the string under this line.
Thank you.
Hopefully someone can help with a solution.
Edit Alright thanks guys, I'll try to figure it out, probably going to just rewrite the whole file. Ok well the program I am making is related to the hosts file, and not everyone has the same hosts file, so I was wondering if there is a way to read their hosts file, and copy all of it, while adding the string to it?
With regular files there's no way around it - you must read the text that follows the line you wish to append after, overwrite the file, and then append the original trailing text.
Think of files on disk as arrays - if you want to insert some items into the middle of an array, you need to shift all of the following items down to make room. The difference is that .NET offers convenience methods for arrays and Lists that make this easy to do. The file I/O APIs offer no such convenience methods, as far as I'm aware.
When you know in advance you need to insert in the middle of a file, it is often easier to simply write a new file with the altered content, and then perform a rename. If the file is small enough to read into memory, you can do this quite easily with some LINQ:
var allLines = File.ReadAllLines( filename ).ToList();
allLines.Insert( insertPos, "This is a new line..." );
File.WriteAllLines( filename, allLines.ToArray() );
This is the best method to insert a text in middle of the textfile.
string[] full_file = File.ReadAllLines("test.txt");
List<string> l = new List<string>();
l.AddRange(full_file);
l.Insert(20, "Inserted String");
File.WriteAllLines("test.txt", l.ToArray());
one of the trick is file transaction. first you read the file up to the line you want to add text but while reading keep saving the read lines in a separate file for example tmp.txt and then add your desired text to the tmp.txt (at the end of the file) after that continue the reading from the source file till the end. then replace the tmp.txt with the source file. at the end you got file with added text in the middle :)
Check out File.ReadAllLines(). Probably the easiest way.
string[] full_file = File.ReadAllLines("test.txt");
List<string> l = new List<string>();
l.AddRange(full_file);
l.Insert(20, "Inserted String");
File.WriteAllLines("test.txt", l.ToArray());
If you know the line index use readLine until you reach that line and write under it.
If you know exactly he text of that line do the same but compare the text returned from readLine with the text that you are searching for and then write under that line.
Or you can search for the index of a specified string and writ after it using th escape sequence \n.
As others mentioned, there is no way around rewriting the file after the point of the newly inserted text if you must stick with a simple text file. Depending on your requirements, though, it might be possible to speed up the finding of location to start writing. If you knew that you needed to add data after line N, then you could maintain a separate "index" of the offsets of line numbers. That would allow you to seek directly to the necessary location to start reading/writing.
I am designing a crawler which will get certain content from a webpage (using either string manipulation or regex).
I'm able to get the contents of the webpage as a response stream (using the whole httpwebrequest thing), and then for testing/dev purposes, I write the stream content to a multi-line textbox in my ASP.NET webpage.
Is it possible for me to loop through the content of the textbox and then say "If textbox1.text.contains (or save the textbox text as a string variable), a certain string then increment a count". The problem with the textbox is the string loses formatting, so it's in one long line with no line breaking. Can that be changed?
I'd like to do this rather than write the content to a file because writing to a file means I would have to handle all sorts of external issues. Of course, if this is the only way, then so be it. If I do have to write to a file, then what's the best strategy to loop through each and every line (I'm a little overwhelmed and thus confused as there's many logical and language methods to use), looking for a condition? So if I want to look for the string "Hello", in the following text:
My name is xyz
I am xyz years of age
Hello blah blah blah
Bye
When I reach hello I want to increment an integer variable.
Thanks,
In my opinion you can split the content of the text in words instead of lines:
public int CountOccurences(string searchString)
{
int i;
var words = txtBox.Text.Split(" ");
foreach (var s in words)
if (s.Contains(searchString))
i++;
return i;
}
No need to preserve linebreaks, if I understand your purpose correctly.
Also note that this will not work for multiple word searches.
I do it this way in an project, there may be a better way to do it, but this works :)
string template = txtTemplate.Text;
string[] lines = template.Split(Environment.NewLine.ToCharArray());
That is a nice creative way.
However, I am returning a complex HTML document (for testing purposes, I am using Microsoft's homepage so I get all the HTML). Do I not have to specify where I want to break the line?
Given your method, if each line is in a collection (Which is a though I had), then I can loop through each member of the collection and look for the condition I want.
If textbox contents were returned with line-breaks representing where word-wrapping occurs, that result will be dependant on style (e.g. font-size, width of the textbox, etc.) rather than what the user actually entered. Depending on what you actually want to do, this is almost certainly NOT what you want.
If the user physically presses the 'carriage return / enter' key, the relevant character(s) will be included in the string.
Why do you need to have a textbox at all? Your real goal is to increment a counter based on the text that the crawler finds. You can accomplish this just by examining the stream itself:
Stream response = webRequest.GetResponse().GetResponseStream();
StreamReader reader = new StreamReader(response);
String line = null;
while ( line = reader.ReadLine() )
{
if (line.Contains("hello"))
{
// increment your counter
}
}
Extending this if line contains more than one instance of the string in question is left as an exercise to the reader :).
You can still write the contents to a text box if you want to examine them manually, but attempting to iterate over the lines of the text box is simply obscuring the problem.
The textbox was to show the contents of the html page. This is for my use so if I am running the webpage without any breakpoints, I can see if the stream is visually being returned. Also, it's a client requirement so they can see what is happening at every step. Not really worth the extra lines of code but it's trivial really, and the last of my concerns.
The code in the while loop I don't understand. Where is the instruction to go to the next line? This is my weakness with the readline method, as I seldom see the logic that forces the next line to be read.
I do need to store the line as a string var where a certain string is found, as I will need to do some operations (et a certain part of the string) so I've always been looking at readline.
Thanks!