|
Hi,
Im trying to find out the simplest way to store variable values in a file (text or otherwise), as well as being able to read specific areas of the file in order to retrive the values and implement them into a windows form control like a textbox or something. The StreamReader and StreamWriter class works but I dont know how to read specific lines like line number 4 or so. Here is my code:
<pre>
StreamReader sr = new StreamReader("configFile.txt", System.Text.Encoding.Default);
string s = null;
while ((s = sr.ReadLine()) != null)
{
if (s.IndexOf("Name:") != -1)
{
// textBox1.text = s;
break;
}
}
sr.Close();
</pre>
The issue is that I cant select what to search for by line number. All i need is a simple way to add textbox values to a text file where each texbox has its own line in the text file, and I can call up the text file at a certain line of the file and read the contents/make it the textbox text. The data is numerical. I came across hash tables but I do not know how to implement them/save them to a file.
|
|
|
|
|
One approach is to use a xml file.
Lots of examples on the web of how to do this.
Try looking at this[^]
|
|
|
|
|
Awesome advice! Thnks! Here is a website for anyone who is curious as to how to work with XML from the gorund up. Very comprehensive tutorials.
http://www.c-sharpcorner.com/uploadfile/mahesh/readwritexmltutmellli2111282005041517am/readwritexmltutmellli21.aspx
|
|
|
|
|
the simplest solution probably goes like so:
- keep key,value pairs in the file, one per line, all keys unique, key and value separated by some symbol (= seems the obvious choice);
- read all lines with File.ReadAllLines
- inside a foreach loop, split the line using string.Split('=',2), use the first part as a key, the second as a value, and stuff that into a Dictionary
Done. Less than 10 lines of code.
|
|
|
|
|
Possibly dumb question but how do I use the key you stated? I know its an identifier for the value string but do i just use my code to search for it? Like <code> if (s.IndexOf("fn:") != -1) </code> ? or what?
|
|
|
|
|
You create the dictionary like so (omitting error handling):
Dictionary<string, string> dict=new Dictionary<string, string>();
foreach(string s in File.ReadAllLines(path)) {
string parts[]=string.Split('=', 2);
dict.Remove(parts[0]);
dict.Add(parts[0], parts[1]);
}
You look up a key's value by using the key as an index to your dictionary, like so:
string name=dict["Name"];
You enumerate all key-values (maybe not in original order!) like so:
foreach (KeyValuePair<string, string> kvp in dict) {
log(kvp.Key+"="+kvp.Value);
}
You could also prepopulate the dictionary with default values for some keys:
dict.Add("Name", "Jef");
dict.Add("Occupation", "Software Engineer");
|
|
|
|
|
|
Is the file physically saved anywhere in order for me to acess it later? if so, how do I bring up a dictionary object from a file?
|
|
|
|
|
Hi,
you could save settings to disk by creating a loop, similar to the foreach I have shown; and later reload just like you load the first time.
PS: replying to yourself, I did not get a notification and saw your message only by accident.
|
|
|
|
|
I am testing out using Exchange Webservices to post tasks to the current user in a corporate intranet.
The code below works when I run it through Visual Studio 2008 debug mode, but when I publish it to the intranet site, it bails with the following error:
Microsoft.Exchange.WebServices.Data.ServiceResponseException:
When making a request as an account that does not have a mailbox, you must specify the mailbox primary SMTP address for any distinguished folder Ids.
I have disabled anonymous access to the website and have it set security to Integrated Windows Authentication.
As you can see below, I am passing the default credentials to the web service.
What am I missing here?
Any help will be appreciated.
public class ExchangeTask
{
ExchangeService myService = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
public ExchangeTask()
{
ServicePointManager.ServerCertificateValidationCallback = delegate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
};
myService.UseDefaultCredentials = true;
myService.AutodiscoverUrl(EmailAddressToFind);
ServiceURL = myService.Url.ToString();
}
public void SendTask()
{
Task myTask = new Task(myService);
myTask.Body = "This is a reminder task that has automatically been set by ExchangeWebservices";
myTask.DueDate = DateTime.Now;
myTask.ReminderDueBy = DateTime.Now;
myTask.StartDate = DateTime.Now;
myTask.Subject = "Event Reminder Task";
myTask.Save();
}
|
|
|
|
|
Just in case anyone ever looks at this question again,
The solution was to put <identity impersonate="true" /> in the web.config file under <system.web>
|
|
|
|
|
I'm having same problem.
You can try adding: <identity impersonate="true"> in the web config.
If I hardcode a user, password, and domain it works fine, but trying to get cached credentials seems hopeless.
|
|
|
|
|
I have the following xml:
<Resume>
<Details>
</Details>
<Contacts>
</Contacts>
</Resume>
how can I add childs to the Contacts using C# but with referring to the <Contacts> by name not by ID?
I want to add:
<Contacts>
<Contact ID="xxxxxxxx">
<Category>my category</Category>
<Number>my contact number</Number>
</Contact>
</Contacts>
|
|
|
|
|
|
You can use c# DOM to access the XML or DHMTL.
Try this:
http://progzoo.net/wiki/C%23:Navigating_over_DOM_Tutorial
modified 27-May-14 4:40am.
|
|
|
|
|
As I already said, look to XPath.
If you load your XML into an XmlDocument, you can get the desired contact by doing:
xmlDoc.SelectSingleNode("/Contacts/Contact[@ID='xxxxxxxxx']");
Then, you have a reference to the node into which you can add subnodes.
|
|
|
|
|
Hi,
I am fairly new to programming and I am making member registration apllication.
The main form contains a DataGridView with all the checked in members. The DataGridView connects to a
stored procedure. This main form is also the start-up form.
In the search results form I can check in / out a member. This done by clicking a button that calls a stored procedure.
Now for my question:
After I click the checkin / out button I want to update the DataGridView in the main form.
I don't seem to be able to make this work.
Below is the code that I use.
The following method is placed on the main form. This is the method I want to call from the other form.
public void FillDataGrid()
{
this.aLL_CHECKED_IN_MEMBERTableAdapter.Fill(this.on_target_v1DataSet.ALL_CHECKED_IN_MEMBER);
}
I want to call this method through the following code, this code is located on a different form:
public void btnCheckIn_Click(object sender, EventArgs e)
{
Procedures.CallStoredProcedure.CHECK_IN_MEMBER(this.lblNumber.Text);
CheckInOut(this.lblNumber.Text);
frmMain cls = new frmMain();
cls.FillDataGrid();
}
Does anyboady have any ideas how I can tackle this?
Regards,
Paul Stol
|
|
|
|
|
|
Using events as mentioned above. See this tip[^] and this article[^].Dave
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Hi to all! I'm having the following problem and I would like some help on trying to solve it.
I'm trying to correct a problem I detected in a code from a project I'm working on (the code is not mine and since the person who made it is no longer in the team I have no way of knowing why this was made this way).
I've spoted a cast from double to long that doesn't seem to work out very well. The idea is to convert the double value to a long so that it can be compared between two other longs (max and minimum values in a specific scale). The code is something like this:
minValue = 56;
maxValue = 70;
value = 55.99.
if ((long) value >= minValue && (long) value <= maxValue)
{
....
}
The problem is that the cast makes value to be 55, instead of 56. Checking other alternatives, I saw Convert.ToInt64, that, for this example works. However, it does not work the way I wanted if the value is say, 70.5, in this case I'm stuck with 70, when I would like to have 71, like I saw on MSDN ("If value is halfway between two whole numbers, the even number is returned; that is, 4.5 is converted to 4, and 5.5 is converted to 6.").
Can anybody point me to the correct way of converting a double to long, without this problem with rounding the even numbers?
Thanks in advance!
|
|
|
|
|
I know it doesn't answer the question exactly as it was asked but...have you thought of reversing the conversion for comparison?
if(value >= (double)minValue && value <= (double)maxValue) "I need build Skynet. Plz send code"
|
|
|
|
|
That won't work if he does math on either of the values..45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
You may have to round the number using one of the three rounding methods:
Math.Round which rounds to the nearest whole number
Math.Ceiling which rounds to the next highest whole number
Math.Floor which rounds to the next lowest whole number
Your comparison should be something like this:
if (Math.Round(value) >= minValue && Math.Round(value) <= maxValue)
{
} .45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
That doesn't make much sense to me. I'll have to guess what you intent. You not showing the declarations does not help.
If all of those variables are doubles, then compare doubles, unless your conditional block is really going to do something with (long)value . If so, it deserves to be represented by a local value and an all-long comparison is in order, that would yield:
long minValue = 56;
long maxValue = 70;
double value = 55.99.
...
long longValue=(long)value;
if (longValue >= minValue && longValue <= maxValue) {
do_something_with(longValue);
}
|
|
|
|
|
Luc Pattyn wrote: That doesn't make much sense to me.
Just pray it's not the code that's calculating your credit card bill. txtspeak is the realm of 9 year old children, not developers. Christian Graus
|
|
|
|