|
Long winded, but as a demo...
static void SumNumbersinDate()
{
string theDate = "19881205";
char[] dateLetters = theDate.ToCharArray();
int[] dateNumbers = new int[theDate.Length];
int sum = 0;
for (int ind = 0; ind < theDate.Length; ind++)
{
Console.WriteLine("Current Total: " + sum.ToString());
dateNumbers[ind] = int.Parse(dateLetters[ind].ToString());
Console.WriteLine("Adding: " + dateNumbers[ind].ToString());
sum += dateNumbers[ind];
Console.WriteLine("New Total: " + sum.ToString());
}
Console.WriteLine("The sum of the numbers is: " + sum.ToString());
Console.WriteLine("Press any key to close the demo...");
Console.ReadKey(true);
}
|
|
|
|
|
|
Integer.TryParse is often the best solution since it ensures you are parsing an integer and if not, does not throw an error.
|
|
|
|
|
Do you also have to verify that a date is valid?
I'd hazard a guess that maybe you should.
Garbage-In = Garbage-Out
Q. Hey man! have you sorted out the finite soup machine?
A. Why yes, it's celery or tomato.
|
|
|
|
|
Since OP needs this to calculate a "lucky number" I wouldn't worry too much about "garbage".
"I had the right to remain silent, but I didn't have the ability!"
Ron White, Comedian
|
|
|
|
|
|
Don't bump your question, it is bad manners.
|
|
|
|
|
It's also spectacularly stupid when your question is already at the top of the list...
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
|
|
|
|
|
I did wonder if he double submitted, but the I realised the site protects against this and the time-stamp was 10 minutes apart.
[Edit]
I'm back off to bed, I've lost the ability to type.
|
|
|
|
|
You might wanted to enter the text here[^].
|
|
|
|
|
|
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
|
|
|
|
|
I kindly ask you to not to repost.
|
|
|
|
|
obviously a member of a rarefaction. (it's an audio joke)
Q. Hey man! have you sorted out the finite soup machine?
A. Why yes, it's celery or tomato.
|
|
|
|
|
Hey Guys, I am kind of looking around at a couple different places for answers here, got some help from some great folks here before so here goes!
I am working on, same project as I have always been working on, just adding more stuff lol. In this project I want to call an ini setting into a combo box. Here's where I am, I can list the directories into a comboBox, and I can read a line from an ini from a single location into a combo box. Now, I need to take this one step further. I need to list that ini setting in the comboBox, from within all those directories. All ini files are named the same thing, but the context is different in the ini field. So basically, I have two pieces two a puzzle figured out, that I need to make fit. Here's an example of what I can do, and what I want it to do.
//I can list the directories into a combo box using this
//Inside the FOLDERS directory are three folders, Folder1, Folder2, and Folder3.
//They are what is displayed in the comboBox.
DirectoryInfo obj = new DirectoryInfo("C:\\Users\\first.last\\Desktop\\Projects\\Test Files\\Folders\\");//you can set your directory path here
DirectoryInfo[] folders = obj.GetDirectories();
comboBox.DataSource = folders; // Populate combobox
Ok, so that's simple enough. What I want to do is take the values of my choice from the code below and list them from EACH directory into the comboBox. There could be any numbers of folders in that directory, but that shouldn't matter.
Here is how I show my ini settings now
IniFile ini = new IniFile("C:\\Users\\first.last\\Desktop\\Projects\\Test Files\\Folders\\Folder1\\Settings.txt");
comboBox.Text = ini.IniReadValue("Info", "Name");
Here's what I WANT to be populated in the comboBox at time of load.
IniFile ini = new IniFile("C:\\Users\\first.last\\Desktop\\Projects\\Test Files\\Folders\\" + Search.All.Folders.For + "\\Settings.txt");
comboBox.Text = ini.IniReadValue("Info", "Name");
I am not sure if that is even how it would be done, but I want to list that value in the combo box, for each folder that has that settings.txt.
If I need to clarify anything, please let me know, I can also provide a complete example of my source code if someone needs it zipped up for them. Thank you!
EDIT:
So I see I have gotten quite a few views of this and maybe it is a bit confusing. Here is how I theorize this happening, but needing guidance on making it a reality.
Couldn't I define folders as a string, and do something like
For Each (Folder) //(meaning it will do this block of code for each item in the string, being the folders in the specified directory)
{
IniFile ini = new IniFile("C:\\Users\\first.last\\Desktop\\Projects\\Test Files\\Folders\\" + Folder + "\\Settings.txt");
comboBox.Text = ini.IniReadValue("Info", "Name");
}
Edit #2 - I am going back through my C# tutorials because I remembered something that I learned about the while statement, I thought I could apply it in this case but am having no luck making "folders" turn into a string to even remotely make this possible. I am referring to www.learnvisualstudio.net video number 2010_02_06.
Here was my theory....
string thisFolder = folders; //I can't make this a string because it is already declared and I can't convert it to a sting implicitly...
while (folders != null)
{
//execute
IniFile ini = new IniFile("C:\\Users\\first.last\\Desktop\\Projects\\Test Files\\Folders\\" + thisFolders + "\\Settings.txt");
if (folders = null)
{
comboBox.Text = ini.IniReadValue("Info", "Name");
}
}
In my head this sounded as if it would have worked. But this just isn't the way things work lol...
modified 10-Mar-13 7:23am.
|
|
|
|
|
I'm not absolutely clear what you are trying to do - it may be that I need more coffee - but I think you are trying to supply a root folder and access all the files under that that are called "settings.txt", then read a setting value from each of those and display them all in a combobox.
If so, then I think you are trying to force the wrong methods to do the job. Lets just step back a litle and look at the task:
Find all files called "settings.txt" somewhere in a folder heiracrchy.
Well, you are locating att teh top level folders under the root ok with DirectoryInfo.GetDirectory, but that isn't quite what you want. Not only does it return a DirectoryInfo object (which contains the string you think you want in the FullName property) but there is a much, much easier way to do exactly what you want:
string[] SettingsFiles = Directory.GetFiles(@"D:\Temp\", "Settings.txt", SearchOption.AllDirectories);
foreach (string file in SettingsFiles)
{
Console.WriteLine(file);
}
You can then simply read each file since you have it's full path and name.
Doesn't this do what you want?
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
|
|
|
|
|
I completely boogered my project. I have to rewrite everything and test what you got there. I think I need to go one step further with what you gave me there though. It looks like that just gives me the paths to the directories that contain a file called settings.txt, which is great thats the first step.
Here's a layout of sorts to illustrate what I want to accomplish.
There are 3 folders with the settings file.
D:\Temp\Folder1\Settings.txt
D:\Temp\Folder2\Settings.txt
D:\Temp\Folder3\Settings.txt
in the text file, I have settings that look like this
[Name]
name=
and the ini code I have up there grabs the verbage in that section. So let's say the names are like this:
Folder1 - name=John
Folder2 - name=Sam
Folder3 - name=Zach
I want the comboBox to have a list like this:
John
Sam
Zach
I hope that helps understand my goal. Thank you for your reply! I am working on rebuilding my project right now.
|
|
|
|
|
Goaty65109 wrote: that just gives me the paths to the directories that contain a file called settings.txt
Not quite - if you add a button to your form and run that code in the button click, you will find it prints the full path to all the files called "Settings.txt". So all you have to do is feed the string into your IniFile constructor and access the file:
IniFile ini = new IniFile(file); You can then add each setting value to your ComboBox Items collection. You don't want to set the Text property each time, because that will only show the last value you set into it - it is just a string property after all!
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
|
|
|
|
|
So let me make sure I understand this correctly, and please forgive me as I am completely novice and am running through as many training videos as I can all the while trying to dream big with my own personal projects.
After I generate that information, I just need to plug this into a button (or form_load as intended):
IniFile ini = new IniFile(file);
comboBox = ini.IniReadValue("Info", "Name");
and it will simply fill my combo box with my desired settings? I have a feeling I am wrong as this sounds too simple, or maybe I just hit the jackpot on my biggest hurdle. Thank you so much for working with me.
|
|
|
|
|
Not quite that simple - you need to use both bits of code, and you can't assign a string to a ComboBox variable!
Try something like this:
string[] SettingsFiles = Directory.GetFiles(@"D:\Temp\", "Settings.txt", SearchOption.AllDirectories);
comboBox.Items.Clear();
foreach (string file in SettingsFiles)
{
IniFile ini = new IniFile(file);
comboBox.Items.Add(ini.IniReadValue("Info", "Name"));
}
BTW: you probably don't need the "Ini" part on the method name "IniReadValue" - it is implied by the fact that it's an IniFile method.
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
|
|
|
|
|
You are absolutely amazing, thank you so much. I am still rebuilding, but I will get back with you with my results. Is there any way to give you props on this site? I would like to contribute to whatever the reputation tracker is. Thank you so much!
|
|
|
|
|
You're welcome!
You can vote on (almost) any message - in the forums there is a green up arrow beside the message that adds to the authors rep, and a red one which takes from it.
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
|
|
|
|
|
Question that came into my mind whilst I am close to being done rebuilding. Can I change the SearchOption.AllDirectories into something more specific? In case I copy and paste a directory to make a backup of the original settings? Like for instance, only look in folders that BEGIN with "BRD"? Thanks again.
SearchOption = "\\BRD*"); <--- just an example of what I mean
Edit: Ok it's all built. I am now running into an exception. Apparently, the name IniReadValue does not exist in the current context. But it exists in my ini class.
public string IniReadValue(string Section,string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section,Key,"",temp,255,this.path);
return temp.ToString();
}
|
|
|
|
|
No - the search only considers file names, it doesn't compare against the path. But it's pretty easy do check afterwards:
if (file.Contains(@"\BRD"))
{
...
}
Exception: (I suspect it's a compilation error?)
Show your code (cut'n' paste so you don't mistype - and try using the "code" widget above the text box to format your code - it makes it easier to read)
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
|
|
|
|
|
Here is my main form:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ReadWriteIni;
using Ini;
namespace ReadWriteIni
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string[] settingsFiles = Directory.GetFiles(@"C:\\Users\\Name\\Desktop\\Projects\\test.Files\\Folders\\",
"Settings.txt", SearchOption.AllDirectories);
comboInst.Items.Clear();
foreach (string file in settingsFiles)
{
IniFile ini = new IniFile(file);
comboInst.Items.Add(Ini.IniReadValue("Info", "Name"));
}
}
}
}
And here is my Ini Class (Wish there was a spoiler tag to collapse this part...)
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace Ini
{
public class IniFile
{
public string path;
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
public IniFile(string INIPath)
{
path = INIPath;
}
public void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, this.path);
}
public string IniReadValue(string Section, string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
return temp.ToString();
}
}
}
|
|
|
|
|