Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
in c# i need to compare 2 numbers one from a local file, and one another from a downloaded file like a Patcher.
if I use Streamreader c# sad to me that he can't convert string into INT.
are there a solution for this?
file a contains the value "1" , the file b contains the value "2"
so if b>a then download the new files catch from another updater file.
thanks
If that is the only number in the file, you can use File.ReadAllText (or File.ReadAllLines in a multiline file) and convert to int like this:
string[] lines = File.ReadAllLines(#"c:\t.txt");
int number = Convert.ToInt32(lines[0]);
try to use the Convert.ToInt32 method.
If your file contains olny one number, you could use the File.ReadAllLine method, insted of streamreader.
void CompareVersions()
{
WebClient client = new WebClient();
var serverVersion = client.DownloadString("http://yourwebsite.com/version.txt");
using (StreamReader sr = new StreamReader("file.txt"))
{
if (Convert.ToInt32(serverVersion) > Convert.ToInt32(sr.ReadLine()))
{
// server version bigger
}
else
{
// up to date
}
}
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm reading huge csv files (about 350K lines by file) using this way:
StreamReader readFile = new StreamReader(fi);
string line;
string[] row;
readFile.ReadLine();
while ((line = readFile.ReadLine()) != null)
{
row = line.Split(';');
x=row[1];
y=row[2];
//More code and assignations here...
}
readFile.Close();
}
The point here is that reading line by line a huge file for every day of the month may be slow and I think that it must be another method to do it faster.
Method 1
By using LINQ:
var Lines = File.ReadLines("FilePath").Select(a => a.Split(';'));
var CSV = from line in Lines
select (line.Split(',')).ToArray();
Method 2
As Jay Riggs stated here
Here's an excellent class that will copy CSV data into a datatable using the structure of the data to create the DataTable:
A portable and efficient generic parser for flat files
It's easy to configure and easy to use. I urge you to take a look.
Method 3
Rolling your own CSV reader is a waste of time unless the files that you're reading are guaranteed to be very simple. Use a pre-existing, tried-and-tested implementation instead.
In a simple case (there're no quotation, i.e. '"' within the file) when you expect partial reading, you may find useful
var source = File
.ReadLines(fileName)
.Select(line => line.Split(';'));
for instance if you want to find out if there's a line in CSV such that 3d column value equals to 0:
var result = source
.Any(items => items[2] == "0");
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
I am looking to create a small windows app that will take a text file of hundreds, maybe thousands of lines of text and then randomize the text and print 5 lines seperated by a line break. I should be able to copy from the app and each time I hit the "generate" button it should delete the previous 5 text outputs.
Here's an example:
https://www.random.org/lists/
The difference is that this app randomizes and prints all lines. Could someone point me to some resources on how to do this exact thing?
Microsoft own c# developer portal has examples of how to read from a text file
How to: Read From a Text File (C# Programming Guide) which can get you started with loading the text.
Microsoft's own Developer Network also has information on random number generation and examples at Random Class
finally Microsoft's own ASP.Net ASP.NET Samples has load of examples and information about building web (or desktop) applications.
You should be able to find working examples and API information on all three of these locations, that will help you with your quest of development of C# applications.
//Initialize variables
static Random rnd;
static StreamReader reader;
static List<string> list;
//here we load the text file into a stream to read each line
using (reader = new StreamReader("TextFile1.txt"))
{
string line;
list = new List<string>();
rnd = new Random();
int index;
//read each line of the text file
while (!reader.EndOfStream)
{
line = reader.ReadLine();
//add the line to the list<string>
list.Add(line);
}
//pull 5 lines at random and print to the console window
for (int i = 0; i < 5; i++)
{
index = rnd.Next(0, list.Count);
Console.WriteLine(list[index]);
}
}
Console.ReadKey();
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Consider this code:
int total = 0;
using(var inFile = new StreamReader("text.txt"))
{
string inValue = "";
while ((inValue = inFile.ReadLine()) != null)
{
if(Int32.TryParse(inValue, out number))
{
total += number;
Console.WriteLine("{0}", number);
}
else
Console.WriteLine("{0} - not a number", inValue);
}
}
Console.WriteLine("The sum is {0}", total);
If I do MessageBox.Show("{0}", number);, it gives me an error. Why is this and how can I fix it?
The MSDN states pretty clearly, that MessageBox.Show does take two strings. But those two are not format and parameter, but text and caption. If you want to format your text, use string.Format to format a string before calling the method. You may also use one of the other overloads, but whichever you use, you need to do your own formatting.
Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
This means you need this namespace and you need the dll in your references. Both does not happen by default in a Console Application.
It definitely looks like you are writing a console application and then Messagebox is out of the question. But if you are in fact writing a Windows Forms application, here is the answer:
Messagebox doesn't have a "built-in" formatter, like Console.WriteLine. If you want to format the string you must use String.Format:
MessageBox.Show(String.Format("{0}", number));
Alternatively:
MessageBox.Show(number.ToString());
MessageBox will not work in Console Applications.
The problem is with the line
MessageBox.Show("{0}", number);
You can not call MessageBox.show() in the way you call Console.writeline(). The second parameter in MessageBox.show is MessageBox title. Try writing
MessageBox.Show(number)
or
MessageBox.Show("The sum is "+number);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I have a folder C:\MergeMe\ that is full of tab-delimited text files, but the number of files is unknown at any given time.
How do I read and store the names of the text files in the MergeMe directory into an array?
Use System.IO.Directory.GetFiles(). If you know the extension, you can, for example call:
Directory.GetFiles(#"C:\MergeMe\*.txt")
If you need other options, there are overloads that allow you to provide them.
static void Main(string[] args)
{
foreach (string file in Directory.GetFiles("MyPath"))
{
if (Path.GetExtension(file)=="youExtension")
{
using (StreamReader sr = new StreamReader(file))
{
//Your code
}
}
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to extract the browser name and Operating system in differant strings from
this string. How can I use substring to extract them from
{ Browser/leavethis (Operating System) Leavethis/Leavethis (Leavethis, Leavethis) Leavethis/Leavethis/Leavethis }
Instead of splitting the useragent, you should use the properties already supplied the Asp.net Framework to detect the browser/os.
Use this
to get the browser name Request.Browser.Browser and browser version Request.Browser.MajorVersion
to get the Os name Request.Browser.Platform and os version Request.UserAgent
A regex such as the following should work, but I suspect #bastos.sergio's answer is the correct way to get what you need.
Regex r = new Regex(#"{\s+(?<Browser>\w+)/[^(]+\((?<OperatingSystem>[^)]+).+");
string browser;
string OS;
var match = r.Match(s);
if (match.Success)
{
browser = match.Groups["Browser"].Value;
OS = match.Groups["OperatingSystem"].Value;
}