|
Hi,
I'm quite new to C# and been working on this problem for days and already at my dead end . I have been trying to learn from sample codes from forums and doing lots of cuts and pastes. Hope someone can help.
I have a Windows Service where I am getting data from web, write it to a .txt file and email at scheduled time.
I can send scheduled email alone. But when I tried to extract data from web, write that data in .txt file and email that data as scheduled altogether, the program hangs at new StreamWriter declaration. I used eventLog1.WriteEntry() to check where it stops. There is no error message. The program just hangs there. I really don't know what is happening. Can someone help? This is what I have done so far ..
protected override void OnStart(string[] args)
{
System.Timers.Timer time = new System.Timers.Timer();
time.Start();
time.Interval = (1000 * 60 * 5);
time.Elapsed += new ElapsedEventHandler(time_elapsed);
time.AutoReset = true;
time.Start();
}
public void time_elapsed(object sender, ElapsedEventArgs e)
{
DateTime scheduledRun = DateTime.Today.AddHours(13.20);
DateTime lastRan = lastTime.LastWriteTime;
if (DateTime.Now > scheduledRun)
{
TimeSpan sinceLastRun = DateTime.Now - lastRan;
if (sinceLastRun.Minutes > 5)
{
importFrmURL("http:// ..", "@"\Import1.txt"");
}
}
}
public void importFrmURL(string url, string dir)
{
System.IO.StreamWriter fileWriter;
fileWriter = new System.IO.StreamWriter(dir);
try
{
var client = new WebClient();
using (var stream = client.OpenRead(url))
using (var reader = new StreamReader(stream))
{
string line;
while ((line = reader.ReadLine()) != null)
{
fileWriter.WriteLine(line);
}
fileWriter.Close();
}
}
catch (Exception e)
{
eventLog1.WriteEntry("Error occurred!\n\r" + e.ToString());
}
}
modified 28-Nov-13 2:28am.
|
|
|
|
|
Use a full path instead of the filename only, e.g. @"C:\Temp\Import1.txt".
Also, I'd suggest to create a FileStream first, and then write into that stream:
using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
{
using (StreamWriter writer = new StreamWriter(fs))
{
}
}
|
|
|
|
|
Yes this works! Can you please explain to me what happened when I don't do FileStream? Or if there is an article I can read from.
|
|
|
|
|
Well, the parameters of the FileStream constructor mean that you can create the file when it does not exist already. Don't know for StreamWriter when the file does not yet exist.
|
|
|
|
|
But this doesn't work when I try to save this .txt file in a server drive (e.g. S:\). It is connected and I can see the S drive in my computer but why can't I write there? Can't I just treat it like any other folder and save the text file under @"S:\Project1\Import1.txt" etc?
|
|
|
|
|
A mapped drive requires authentication, and your windows service is probably not running under the users' credentials.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Something else to be aware of - when you are running a Windows Service, it may not actually know there's an S drive. This is a mapped drive designation that applies to a particular account - unless you set the service to run as that account, it will not know about it.
|
|
|
|
|
I have an object which mimics a table structure in sql. Lets call this object validation. Validation has a property isrequired with a get and set.
when i use this property in code i.e.
validation val = new validation
and then try to change the value of isrequired intellisense says i can not assign to this isrequired because it is read only.
I have a user that wants some items for some objects to be required where as other objects isrequired is false. The table that this object mimics has fieldname and isrequired either true or false. in this case the database has isrequired as true. I just want to change the isrequired in the object and not affect change in the database table.
when i use form.fieldname.isrequiered = false it tells me I can not assign because it is read only. Thanks.
|
|
|
|
|
I would suggest changing it from readonly to read/write.
Veni, vidi, abiit domum
|
|
|
|
|
Thank you so much for your response. where would that change take place?
something like form.fieldname.isrequired.attribute = read/write?
|
|
|
|
|
Unless you show the code where the variable's properties are declared it's anyone's guess.
Veni, vidi, abiit domum
|
|
|
|
|
Pretty basic property establishment
[XmlAttribute("IsRequired")]
[Bindable(true)]
public bool? IsRequired
{
get { return GetColumnValue<bool?>(Columns.IsRequired); }
set { SetColumnValue(Columns.IsRequired, value); }
}
Does this help?
|
|
|
|
|
seeker62 wrote: Does this help? I'm afraid not; I don't recognise what that code is doing.
Veni, vidi, abiit domum
|
|
|
|
|
Ok thanks for your input. I am not sure what else I can provide. I am sure it has something to do with the fact the object is directly correlated to the table in sql useing subsonic to create the object of the table. Again thanks for your input.
|
|
|
|
|
seeker62 wrote: useing subsonic to create the object of the table. No idea what that is. If it's a third party library then you should probably try their website.
Veni, vidi, abiit domum
|
|
|
|
|
This looks like you are getting the Column Value with GetColumnValue call. Not sure what Columns.IsRequired is, another property? My guess is that it comes from the table data. If that is correct then you are trying to set data in the table column with your setter.
Your problem seems to be that Columns.IsRequired is set to read only, not your IsRequired property.
The only way that I know of to set a class property read only is to not give it a setter.
|
|
|
|
|
or maybe also make the setter private
|
|
|
|
|
I am trying to develop a tool which will add a host into the DNS server calling dnscmd.exe. I am assigning the property FileName as "dnscmd.exe" and pass the required i/p parameters. I did call nslookup.exe same way as I have called dnscmd.exe but in case of nslookup.exe code is working properly but in case of dnscmd.exe it's throwing exception while starting the process (here p.Start()) with message as "The system cannot find the file specified" .I am sharing the code snippet in top.
I have uncomment the WorkingDirectory and FileName (with fully qualified path of dnscmd) but still got the the same error. Someone please reply on my post and provide the resolution as I'll implement it ASAP. Thanks in advance
Public bool AddtoDns(IStore iStr)
{
#region "Comment for Testing"
bool isAdded = true;
string strTid = "T00000013";
string strDnsServerName= "OD-EV-W-DC-5.7-11T.COM";
string strZoneName= "7-11t.com";
string strIP= "10.172.13.10"
System.Diagnostics.Process p = new System.Diagnostics.Process();
try
{
if (!CheckDnsEntry(iStr))
{
p.StartInfo.FileName = "dnscmd.exe";
p.StartInfo.Arguments = strDnsServerName + " " + "/RecordAdd" + " " + strZoneName + " " + strTid + " " + "A" + " " + strIP;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.WaitForExit();
isAdded = true;
}
else
{
isAdded = false;
}
return isAdded;
}
catch (Exception ex)
{
throw ex;
}
finally
{
p.Dispose();
}
#endregion
}
|
|
|
|
|
The most obvious thing to check is: "is 'dnscmd' installed, available, and on the system path?"
For example: if you run up a command box and type "dnscmd" into it, does it run? It doesn't on my computer...
The only instant messaging I do involves my middle finger.
English doesn't borrow from other languages.
English follows other languages down dark alleys, knocks them over and goes through their pockets for loose grammar.
|
|
|
|
|
yes dnscmd.exe installed in my computer under c:\windows\system32 (OS-Windows7). Now I am just trying to execute dns command using the below code-
private void button3_Click(object sender, EventArgs e)
{
Process.Start("C:\\Windows\\System32\\cmd.exe", "/K \"dnscmd\"");
}
The above single line code works fine when we replace dnscmd to nslookup,ipconfig etc but when I am trying to run the single line code with dnscmd it opens a cmd window and showing "not recognized as an internal or external command" message but if I open a cmd window from run (not from c# programe) in the same computer (working fine from all directory) then dnscmd works fine and able to add dns entry also.
So, is there any possibility on access issue or anything else which causing abnormal behaviour of dnscmd. Please help!!!!
Subhendu
|
|
|
|
|
The command window should stay open - so try it and then do two things:
1) type "dnscmd" and see if it runs.
2) type "path" and see if what you get looks sensible (i.e. it includes the folder dnscmd.exe is installed in)
I tried it here (with "whoami") and everything looks fine.
The only instant messaging I do involves my middle finger.
English doesn't borrow from other languages.
English follows other languages down dark alleys, knocks them over and goes through their pockets for loose grammar.
|
|
|
|
|
How to give C# chart height ,width based on data?
|
|
|
|
|
You own the data and you own the chart so devise a measurement metric based on the content of the data and set the chart width based on that metric.
We are not telepathic, nor can we see your data so we cannot guess what it looks like so can't devise a metric that would make sense!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
my code
int Xcount = dt.Rows.Count * 25;
List<int> levels = dt.AsEnumerable().Select(al => al.Field<int>(dt.Columns[1].ToString())).Distinct().ToList();
int yCount = levels.Max()*500;
Chart chart = new Chart();
//chart.Size = new Size(Xcount, yCount);
chart.DataSource = dt;
//chart.Width = 350;
chart.Height = Xcount;
//create serie...
Series serie1 = new Series();
serie1.Name = "Serie1";
serie1.LabelBorderWidth = 400;
serie1.Color = Color.FromArgb(255, 0, 0);
serie1.BorderColor = Color.FromArgb(164, 164, 164);
serie1.ChartType = SeriesChartType.Column;
serie1.BorderDashStyle = ChartDashStyle.Solid;
serie1.BorderWidth = 1;
serie1.ShadowColor = Color.FromArgb(128, 128, 128);
serie1.ShadowOffset = 1;
serie1.IsValueShownAsLabel = true;
serie1.XValueMember = dt.Columns[0].ToString();
serie1.YValueMembers = dt.Columns[1].ToString();
serie1.Font = new Font("Tahoma", 8.0f);
serie1.BackSecondaryColor = Color.FromArgb(0, 102, 153);
serie1.LabelForeColor = Color.FromArgb(255, 255, 255);
serie1.ChartType = SeriesChartType.StackedBar;
serie1["PixelPointWidth"] = "12";
chart.Series.Add(serie1);
//create chartareas...
ChartArea ca = new ChartArea();
ca.BorderWidth = 2;
ca.Name = "ChartArea1";
ca.BackColor = Color.White;
ca.BorderColor = Color.FromArgb(255, 0, 0);
ca.BorderWidth = 0;
ca.BorderDashStyle = ChartDashStyle.Solid;
ca.AxisX = new Axis();
ca.AxisY = new Axis();
string YAxixName = dt.Columns[1].ColumnName;
ca.AxisY.Title = YAxixName;
// Set axis title
chart.ChartAreas.Add(ca);
chart.ChartAreas[0].AxisX.MajorGrid.LineWidth = 0;
chart.ChartAreas[0].AxisY.MajorGrid.LineWidth = 0;
//databind...
chart.DataBind();
series name for each series is not displaying.
|
|
|
|
|
So pick out all the bits that add to the width of a column, multiply that by the number of columns in the data, add a bit and set your with to that. Run your app and when it looks horrible try and work out what you have missed. Get the max and min of your data range and that will help define your height.
Never underestimate the power of human stupidity
RAH
|
|
|
|