The above contains the "parse_me.txt" file, which has the numbers from 1-10 as string
s, 1 per line (one, two ... ten), and the "save to desktop.linq" file, which contains our code. Simply extract both to the desktop, and if you have linqpad installed, just double click the linq file and run it.
Introduction
Assumptions
Following those assumptions, you try to run your query on your Work computer, and realize it fails to work, because the paths are all messed up.
In my case, I keep all my linqpad queries in my documents folder, under the default LINQPad Queries folder. In that folder, I have a "Misc files" folder with some text files I was trying to parse, let's assume it's parse_me.txt.
First, linqpad is being run from C:\ProgramData\LINQPad\Updates40\448 (ymmv). This means you can't use a relative path from where the executable is, and you need to use a full URI to the files. This is why it was encoded as:
var path = "C:\Users\Your.User.Name\Documents\LINQPad Queries\Misc Files\parse_me.txt";
Since I'm keeping the same structure on my machines (so I can just copy the entire queries folder and keep working), the only difference would be the user name on the other machine. So, with this simple workaround, we can have a more generic way to access our file:
var file_name = "parse_me.txt";
var path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
var file_location = Path.Combine(path,file_name);
string[] read_all_lines = System.IO.File.ReadAllLines(file_location);
(For more special folders, have a look at this MSDN page).
Now, you can obviously encode the file name as part of the path, but let's assume you want to write the output of your processing to a file, and you want it to be next to your input file. Now you can do:
var out_file_name = "exciting_results.txt";
file_location = Path.Combine(path,file_name);
System.IO.File.WriteAllLines(file_location, some_string_array);
Console.Out.WriteLine( out_file_name + " saved successfully.");
Done.
Now you can work on both computers, and assuming you have the linq query (or your .NET class / project), and that your input file is in the same "relative" (directory on both machines, your code will work :). In "relative", I mean: both "desktop" folders, or both "my document\my code" folder, etc.
Full Example
Here's a full example using Linqpad and an input / output file combo (If you're not familiar with Linqpad, the .Dump()
acts a bit like you console WriteLine()
, but will print objects as well).
void Main()
{
var file_name = "parse_me.txt";
var path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
var file_location = Path.Combine(path,file_name);
string[] read_all_lines = System.IO.File.ReadAllLines(file_location )
.Dump("This is our input")
;
var even_length = read_all_lines.Where (line =>line.Length %2 == 0 )
.Dump()
;
var uneven_lenght = read_all_lines.Except(even_length)
.Dump()
;
var first_two_uneven_longer_than_4 = uneven_lenght
.Where ( number => number.Length > 4 )
.Take(2)
.Dump();
;
System.IO.File.WriteAllLines( Path.Combine(path,"exciting_results.txt")
, first_two_uneven_longer_than_4 );
}
The content of the "parse_me.txt" that should be on your desktop looks like:
one
two
three
four
five
six
seven
eight
nine
ten
The results will look like:
This is your user name |
Your.User.Name |
This is our input (result of: ReadAllLines) |
5String[] (10 items)4 |
one |
two |
three |
four |
five |
six |
seven |
eight |
nine |
ten |
Numbers with uneven length: |
one |
two |
three |
six |
seven |
eight |
ten |
Hope you'll find it useful. :)
History
- 27th April, 2016: Updated the results and added some explanation about how to use the code
- 27th June, 2014: Initial release
- 27th June, 2014: Update, thanks to SoMad good pointer