所以,我有了這個功能,從INI文件讀取:爲什麼數組比我定義的元素多?
private void GetRigInfo()
{
RigInfo = new string[9];
var fileLocation = new string[2];
// The problem is that there's no telling where the hddrigsite.ini will be
stored. So, we have to find out where it is from the hddconfig.ini.
Log("Locating rig info");
// There's no telling if this will be on a 32 or 64 bit OS. Check for both
var rigInfoLocation = File.ReadAllLines(Environment.Is64BitOperatingSystem ?
@"C:\Program Files (x86)\HDD DrillView\hddconfig.ini" :
@"C:\Program Files\HDD DrillView\hddconfig.ini");
// This should get us the location of the rigsite info we need.
foreach (var s in rigInfoLocation.Where(s => s.Contains("data_dir")))
{
fileLocation = s.Split('=');
}
RigInfo = File.ReadAllLines(fileLocation[1] + "\\hddrigsite.ini");
Log("Rig info found");
}
現在,當我步,並獲得在函數的最後Log()
,我將鼠標懸停在RigInfo
,Visual Studio的智能感知給我看RigInfo{string[30]}
。現在,我一直都明白= new string[9]
會創建一個9元素的數組。那爲什麼它允許有30個元素?當我運行程序時,當涉及到這個數組時,我不會遇到任何錯誤或任何其他問題。事實上,它的工作方式正是我在整體事物方案中所需要的。感謝您幫助理解這是如何以及爲何如此。還附加截圖以獲得更好的視覺幫助。
我知道這不是一個答案你的問題,但是因爲你在.NET中,你爲什麼要使用.ini文件?使用App.Config(或web.config,如果它是一個ASP.NET站點)並使用System.Configuration.ConfigurationManager提取值是***非常容易***,它是「。NET方式「來執行此操作。http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx – David
我的程序訪問由VB6程序創建和使用的INI文件。 – MyCodeSucks
Gotcha!Valid如果沒有冒犯,我只是問。 – David