How to parse part of script code in c#? - c#

I have a task to create an UI to run methods from scripts.
I made a method that shows the names of Methods from Script, and on the UI there is a checkbox next to them.
I have a problem that when I click on the checkbox, I do not know to invite only a pieces of code of this method.
private void GetAllDSCConfigurationName(string psFile, DSCTreeViewItemFolder item)
{
var file = new DSCTreeViewItemScipt() { Script = psFile };
item.AddChild(file);
try
{
using (StreamReader sr = new StreamReader(psFile))
{
string line;
while ((line = sr.ReadLine()) != null)
{
if (line.StartsWith("Configuration"))
{
file.AddChild(new DSCTreeViewItemConfiguration() { ConfigurationName = line });
}
}
}
}
catch (Exception e)
{
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}

You can check in the same if statement:
if(line.StartsWith("Configuration") && line.Contains("LogOff"))
{
// do your stuff
break; // cause you don't need to look for other methods
}
If you have a name of your method from UI you can pass it to Contains method.
Also you can improve a bit your code:
string allLines = sr.ReadToEnd();
string configurations = allLines.Split("Configuration");
string methodToCall = configurations.FirstOrDefault(conf => conf.Contains("LogOff")); // or you can pass name of your method from parameter

Related

Assigning instance variables obtained through reflection in generic method

I have data in tab-separated values (TSV) text files that I want to read and (eventually) store in database tables. With the TSV files, each line contains one record, but in one file the record can have 2 fields, in another file 4 fields, etc. I wrote working code to handle the 2-field records, but I thought this might be a good case for a generic method (or two) rather than writing new methods for each kind of record. However, I have not been able to code this because of 2 problems: I can't create a new object for holding the record data, and I don't know how to use reflection to generically fill the instance variables of my objects.
I looked at several other similar posts, including Datatable to object by using reflection and linq
Below is the code that works (this is in Windows, if that matters) and also the code that doesn't work.
public class TSVFile
{
public class TSVRec
{
public string item1;
public string item2;
}
private string fileName = "";
public TSVFile(string _fileName)
{
fileName = _fileName;
}
public TSVRec GetTSVRec(string Line)
{
TSVRec rec = new TSVRec();
try
{
string[] fields = Line.Split(new char[1] { '\t' });
rec.item1 = fields[0];
rec.item2 = fields[1];
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("Bad import data on line: " +
Line + "\n" + ex.Message, "Error",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Error);
}
return rec;
}
public List<TSVRec> ImportTSVRec()
{
List<TSVRec> loadedData = new List<TSVRec>();
using (StreamReader sr = File.OpenText(fileName))
{
string Line = null;
while ((Line = sr.ReadLine()) != null)
{
loadedData.Add(GetTSVRec(Line));
}
}
return loadedData;
}
// *** Attempted generic methods ***
public T GetRec<T>(string Line)
{
T rec = new T(); // compile error!
Type t = typeof(T);
FieldInfo[] instanceVars = t.GetFields();
string[] fields = Line.Split(new char[1] { '\t' });
for (int i = 0; i < instanceVars.Length - 1; i++)
{
rec. ??? = fields[i]; // how do I finish this line???
}
return rec;
}
public List<T> Import<T>(Type t)
{
List<T> loadedData = new List<T>();
using (StreamReader sr = File.OpenText(fileName))
{
string Line = null;
while ((Line = sr.ReadLine()) != null)
{
loadedData.Add(GetRec<T>(Line));
}
}
return loadedData;
}
}
I saw the line
T rec = new T();
in the above-mentioned post, but it doesn't work for me...
I would appreciate any suggestions for how to make this work, if possible. I want to learn more about using reflection with generics, so I don't only want to understand how, but also why.
I wish #EdPlunkett had posted his suggestion as an answer, rather than a comment, so I could mark it as the answer...
To summarize: to do what I want to do, there is no need for "Assigning instance variables obtained through reflection in generic method". In fact, I can have a generic solution without using a generic method:
public class GenRec
{
public List<string> items = new List<string>();
}
public GenRec GetRec(string Line)
{
GenRec rec = new GenRec();
try
{
string[] fields = Line.Split(new char[1] { '\t' });
for (int i = 0; i < fields.Length; i++)
rec.items.Add(fields[i]);
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("Bad import data on line: " + Line + "\n" + ex.Message, "Error",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Error);
}
return rec;
}
public List<GenRec> Import()
{
List<GenRec> loadedData = new List<GenRec>();
using (StreamReader sr = File.OpenText(fileName))
{
string Line = null;
while ((Line = sr.ReadLine()) != null)
loadedData.Add(GetRec(Line));
}
return loadedData;
}
I just tested this, and it works like a charm!
Of course, this isn't helping me to learn how to write generic methods or use reflection, but I'll take it...

The last value in Read.Line() StreamReader

Tell me please, how can I do so that would only give the last meaning of this search.
At the moment, about 15 MessageBox.Show is opened.
How to make it so that only the latter would show?
For the fifth hour I am suffering with different variations. Nothing happens.
TextReader tr = null;
try
{
File.Copy(SteamLogFilePath, tmpFile, true);
}
catch { }
try
{
tr = new StreamReader(tmpFile);
try
{
string line = null;
while (true)
{
line = tr.ReadLine();
if (line.Contains("RecvMsgClientLogOnResponse") && line.Contains("OK"))
{
tmpSplit1 = line.Split(')');
string SteamIdbrut = tmpSplit1[1];
tmpSplit1 = SteamIdbrut.Split(']');
string SteamIdnet = tmpSplit1[0].Replace(" : [", "");
long steam32 = Convert.ToInt64((GetFriendID(SteamIdnet)));
MessageBox.Show((FromSteam32ToSteam64(steam32)).ToString());
}
}
}
catch { }
}
catch { }
if (tr != null)
tr.Close();
You could use this instead:
string lastLine = File.ReadLines(tmpFile)
.Where(line => line.Contains("RecvMsgClientLogOnResponse") && line.Contains("OK"))
.LastOrDefault();
// rest of code to create the MessageBox
This code replaces all of your code from 2nd try to tr.Close();.

StreamReader read last line that contains

I am trying to read from a text file that has multiple outputs from when writing to it but when I want to read from the textfile that I already outputted stuff to, I want to choose the last entry(bear in mind each entry when writing has 5 lines and I just want the line containing "Ciphered text:")
But with this it is reading the lines containing that but I cannot work how to make it show only the last entry containing the string I specified.
using System;
using System.IO;
namespace ReadLastContain
{
class StreamRead
{
static void Main(string[] args)
{
string TempFile = #"C:\Users\Josh\Desktop\text2.txt";
using (var source = new StreamReader(TempFile))
{
string line;
while ((line = source.ReadLine()) != null)
{
if (line.Contains("Ciphered Text:"))
{
Console.WriteLine(line);
}
}
}
}
}
}
I would suggest to use LINQ for better readability:
string lastCipheredText = File.ReadLines(TempFile)
.LastOrDefault(l => l.Contains("Ciphered Text:"));
it is null if there was no such line. If you can't use LINQ:
string lastCipheredText = null;
while ((line = source.ReadLine()) != null)
{
if (line.Contains("Ciphered Text:"))
{
lastCipheredText = line;
}
}
It will be overwritten always, so you automatically get the last line that contained it.
You can use Linq:
var text = File
.ReadLines(#"C:\Users\Josh\Desktop\text2.txt")
.LastOrDefault(line => line.Contains("Ciphered Text:"));
if (null != text) // if thereĀ“s a text to print out
Console.WriteLine(text);

delay output of each line to a file

i am bailing a windows 8 phone app.
i have a method that searches for a specific text from a text box and outputs it to a list box if found. I am trying to delay the output of each line written to a list box so i ideally it would be first line,wait 5 seconds, second line, wait 5 seconds,etc. currently it only delays at first then outputs all the lines it finds at once.
suspect.text is what i am searching for
results is the listbox it is sent to.
public void ReadFile()
{
var str = Application.GetResourceStream(new Uri("MACAddresses.txt", UriKind.Relative));
List<string> lines = new List<string>();
using (StreamReader r = new StreamReader(str.Stream))
{
string line;
while ((line = r.ReadLine()) != null)
{
if(line.Contains(Suspect.Text))
{
lines.add(line);
}
}
{
foreach (string output in lines)
{
Thread.sleep(1000);
results.items.add(output);
{
}
That's because you're blocking the UI thread, so it cannot update the ListBox. Solving this issue is good job for async programming:
public async Task ReadFile()
{
var str = Application.GetResourceStream(new Uri("MACAddresses.txt", UriKind.Relative));
List<string> lines = new List<string>();
using (StreamReader r = new StreamReader(str.Stream))
{
string line;
while ((line = r.ReadLine()) != null)
{
if(line.Contains(Suspect.Text))
{
lines.add(line);
}
}
}
foreach (string output in lines)
{
await Task.Delay(1000);
results.items.add(output);
}
}
Please note the async keyword that has been added in front of the method signature, and the return type that has been changed to Task. You'll have to use the await keyboard to properly call the ReadFile method:
await ReadFile(); // Don't forget to use the await keyword!
The method calling ReadFile will have to be marked as async as well, or the code won't compile.

Advance C# ReadLine() to next line in a function call

In my C# app I'm trying to feed into ReadLine() a simple text document with 7 digit strings separated line by line. What I'm attempting to do is grab the next 7 digit string each time the function is called. Here's what I have so far:
string invoiceNumberFunc()
{
string path = #"C:\Users\sam\Documents\GCProg\testReadFile.txt";
try
{
using (StreamReader sr = new StreamReader(path))
{
invoiceNumber = sr.ReadLine();
}
}
catch (Exception exp)
{
Console.WriteLine("The process failed: {0}", exp.ToString());
}
return invoiceNumber;
}
How do I advance to the next line each time the invoiceNumberFunc() is called?
Thanks in advance.
You'd need to keep hold of the StreamReader between calls, either passing it into the method as a new parameter or making it a member variable of the class.
Personally I prefer the idea of it becoming a parameter, so that it never ends up as a member variable - that makes the life-time easier to manage:
void DoStuff()
{
string path = #"C:\Users\sam\Documents\GCProg\testReadFile.txt";
using (StreamReader sr = new StreamReader(path))
{
while (keepGoing) // Whatever logic you have
{
string invoice = InvoiceNumberFunc(sr);
// Use invoice
}
}
}
string InvoiceNumberFunc(TextReader reader)
{
string invoiceNumber;
try
{
invoiceNumber = reader.ReadLine();
}
catch (Exception exp)
{
Console.WriteLine("The process failed: {0}", exp.ToString());
}
return invoiceNumber;
}
You can't, since you create and dispose the stream reader in the function. Two ways come to mind:
You could store the stream reader in a member variable, or read all at once and store an array in a member variable.
Or you make it an iterator method by changing the return type to IEnumerable<string>, and changing the part in the using block to:
while ((invoiceNumber = sr.ReadLine()) != null) {
yield return invoiceNumber;
}
This way, you can call foreach on your invoiceNumberFunc.
You need to use the same StreamReader rather than creating a new one. Each time you create a new one and dispose of the old one you're starting right back at the start of the file.
Try passing the same StreamReader reference in or keeping a record of the position you are at in the stream and using Seek() on the base stream if necessary. I'd recommend the first of these personally.
You need to rework this, so that you're not creating the streamreader inside the method, but rather creating it at the class level, and just using it in the method, then disposing/closing the reader when you are done. Something like:
class MyClass
{
private StreamReader sr;
string invoiceNumberFunc()
{
if (sr == null)
sr = new StreamReader(path);
if (sr.EndOfStream) {
sr.Close();
sr = null;
return string.Empty;
}
try {
return sr.ReadLine();
}
catch(Exception exp) {
Console.WriteLine("Process failed {0}",exp.ToString());
return string.Empty;
}
}
}
In this case, it might also be a good idea to make your class IDisposable so you can verify that the StreamReader gets disposed, and also potentially make "initialize"/"close" routines, instead of doing the initialize and shutdown how I did here.
What you are looking for is the yield command:-
IEnumerable<string> GetInvoiceNumbers()
{
string path = #"C:\Users\sam\Documents\GCProg\testReadFile.txt";
using (StreamReader sr = new StreamReader(path))
{
while (!sr.EndOfStream)
{
yield return sr.ReadLine();
}
}
}
Now you can consume the return of this function with a simple for each:-
foreach(string invoiceNumber in GetInvoiceNumbers())
{
//Do Stuff with invoice number
}
Or get creative with LINQ.
An other way of doing this is to transform your function in an iterator block using the yield return statement
The only thing is to make sure you add a finaly clause to your try and remove the catch as the yield return cannot be used in a naked try / catch. So your code would become:
IEnumerable<String> invoiceNumberFunc()
{
string path = #"C:\Users\sam\Documents\GCProg\testReadFile.txt";
try
{
using ( System.IO.StreamReader sr = new System.IO.StreamReader( path ) )
{
String invoiceNumber;
while ( ( invoiceNumber = sr.ReadLine() ) != null )
{
yield return sr.ReadLine();
}
}
}
finally
{
}
}

Categories

Resources