Reputation: 271
I am writing information to a file, and naming it as "//Average+DateTime.txt"
; I think my code will be easier than me trying to clarify:
String path=bama();
if(path.Contains(".txt")) {
StreamWriter sw=
new StreamWriter(
"C:/Users/msilliman11/Average"
+DateTime.Now.ToString("yyyyMMdd")
+".txt"
);
}
String ElementsNum=RoundedValues.Count.ToString();
DateTime dt=System.DateTime.Now;
using(var NewFile=File.Create(path)) {
using(var writeIt=new StreamWriter(NewFile)) {
writeIt.Write(
"NA"+","
+dt.Hour.ToString()+","+dt.Minute+","
+dt.Day+","+dt.Month.ToString()+","+dt.Year.ToString()+","
+"ALTEST "+","+"ALTEST "+","+heatgrade
+" "+","+" "+","+heatname+","
+DT2.Columns[3].ToString()+","+heatgrade+","
+"OE2"+","+","+","+","+","+","+","+" "+ElementsNum+","
);
foreach(
var pair in
RoundedValues.Zip(
Elements, (a, b) => new {
A=a,
B=b
})) {
writeIt.Write(pair.B.ToString()+","+pair.A.ToString()+",");
}
}
}
The problem I have is that I need to name the "Average"
file, and have the code in the outer using block written into the file ...
I'm pretty bad at explaining these questions, but basically im trying to get the information in the second using block, to be named "4222013Average.txt"
in an output file ...
Upvotes: 0
Views: 123
Reputation: 793
If I understand your question properly, I think this might work better for you.
var toFile = Path.Combine(@"C:\Users\msilliman11\",
string.Format("Average{0}.txt", DateTime.Now.ToString("yyyyMMdd")));
var dt = DateTime.Now;
using (var fs = File.OpenWrite(toFile))
using (TextWriter sw = new StreamWriter(fs))
{
sw.Write(string.Join(",",
"NA",
dt.Hour,
dt.Minute,
dt.Day,
dt.Month,
dt.Year,
"ALTEST ",
"ALTEST ",
heatgrade,
" ",
" ",
heatname,
DT2.Columns[3],
heatgrade,
"OE2",
string.Empty,
string.Empty,
string.Empty,
string.Empty,
string.Empty,
string.Empty,
string.Empty,
" ",
","));
foreach (var pair in RoundedValues.Zip(Elements, (a, b) => new { A = a, B = b }))
{
writeIt.Write(pair.B.ToString() + "," + pair.A.ToString() + ",");
}
}
Upvotes: 1
Reputation: 6953
It's not entirely clear what the problem is, except:
You said you would like to name the file: 4/22/2013Average.txt
. This is not possible. Forward slashes are not allowed in file names.
Is this what you needed to know? If not, please be more clear.
Also a tip: make your code more legible by using string.Format()
:
string text = string.Format("NA,{0},{1},{2},{3},{4},ALTEST,ALTEST ,{5} , ,{6},{7},{5},OE2,,,,,,, {8},",
dt.Hour,
dt.Minute,
dt.Day,
dt.Month,
dt.Year,
heatgrade,
heatname,
DT2.Columns[3],
ElementsNum);
writeIt.Write(text);
Upvotes: 0