Как загрузить параметр из внешнего XML-файла, находящегося в той же папке, что и моя программа? Или я должен использовать старый школьный файл .ini? Это только имя сервера MySQL и имя места, которое мне нужно передать в мое приложение.Загрузка настроек из ini/xml-файла
ответ
Вы должны посмотреть Application Settings и ConfigurationManager. Вы также можете использовать раздел Connection Strings в app.config
.
... Для AppSettings ...
C# код ... Вам потребуется System.Configuration
присвоенный
var value = ConfigurationManager.AppSettings["MySetting"];
App.Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="MySetting" value="My Value"/>
</appSettings>
</configuration>
... Для Строки подключения ...
C# код ... Вам понадобится справка System.Configuration
var value = ConfigurationManager.ConnectionStrings["MySqlConn"];
App.Config
<configuration>
<connectionStrings>
<add name ="MySqlConn"
connectionString="Server=myServerAddress;Database=myDataBase;
Uid=myUsername;Pwd=myPassword;"/>
</connectionStrings>
</configuration>
+1, Бросьте упоминание в раздел ConnectionStrings там, потому что это звучит так, как он хочет. –
@Jimmy, хороший вызов; o) ... Я был посреди этого, когда вы разместили комментарий. Благодарю. –
.NET есть понятие файла настроек, связанных со сборкой.
Если ваше имя сборки:
My.Program.exe
вы можете создать файл настроек для этой сборки, которая автоматически доступны через ConfigurationManager:
My.Program.exe.config
Вы можете легко сделать это часть вашего проект, добавив файл App.config: http://msdn.microsoft.com/en-us/library/ms184658(VS.80).aspx
Он не кажется автоматическим - требуется вызов ConfigurationManager.OpenExeConfiguration. –
Чтобы ответить на ответ Мэтью W, это звучит так, как будто вы необходимо указать Add -> New Item -> Application Configuration File
в вашем проекте. Это создаст app.config (который будет скомпилирован в [executableName] .exe.config).
В окне app.config у вас будет область для ConnectionStrings. Поместите здесь информацию о подключении и вызовите ее, используя ConfigurationManager
.
Спасибо, я надеялся, что ссылка в нижней части моего ответа была достаточно ясной. Возможно, я должен переместить его над образцом кода. –
@Matthew Whited: Да, я отправлял свой ответ так же, как вы редактировали. Скорее всего, сейчас ясно. – AllenG
Я переместил их и сделал их смелыми. Еще раз спасибо. –
Вы можете пойти с новым, но в случае, если вы хотите идти с INI, вот некоторые источники за окна формирует версию:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Runtime.InteropServices;
//After you've added this class code to your application, here's how you may want to use it:
//Dim objclsINI As New clsINI("c:\fName.ini")
//objclsINI.WriteINI("Settings", "ClockTime", "12:59")
//objclsINI.WriteINI("Settings", "ClockTime", "12:59", "c:\test.ini")
//Dim strData As String = objclsINI.ReadINI("Settings", "ClockTime", "(none)")
public class clsINI
{
[DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileStringA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern int GetPrivateProfileString(string lpApplicationName, string lpKeyName, string lpDefault, System.Text.StringBuilder lpReturnedString, int nSize, string lpFileName);
[DllImport("kernel32.dll", EntryPoint = "WritePrivateProfileStringA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern int WritePrivateProfileString(string lpApplicationName, string lpKeyName, string lpString, string lpFileName);
[DllImport("kernel32.dll", EntryPoint = "WritePrivateProfileStringA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern int FlushPrivateProfileString(int lpApplicationName, int lpKeyName, int lpString, string lpFileName);
// API functions
string strFilename;
string strSection;
string strKey;
// Constructor, accepting a filename
public clsINI(string Filename)
{
strFilename = Filename;
}
// Overloaded Constructor Creating The Default FileName
public clsINI()
{
strFilename = Application.StartupPath + "\\" + Application.ProductName + ".ini";
}
// filename property
public string FileName {
get { return strFilename; }
set { strFilename = value; }
}
// Section property
public string Section {
get { return strSection; }
set { strSection = value; }
}
// Key property
public string Key {
get { return strKey; }
set { strKey = value; }
}
public string ReadINI(string Default)
{
string functionReturnValue = null;
// Returns a string from your INI file
int intCharCount = 0;
string strMessage = null;
System.Text.StringBuilder objResult = new System.Text.StringBuilder(256);
strMessage = "";
if (string.IsNullOrEmpty(strKey))
strMessage = "The INI File Class Does Not Have A Defined Key To Read.";
if (string.IsNullOrEmpty(strSection))
strMessage = strMessage + ControlChars.CrLf + "The INI File Class Does Not Have A Defined Section To Read.";
if (!string.IsNullOrEmpty(strMessage)) {
MessageBox.Show(strMessage, "INI Error");
return;
}
intCharCount = GetPrivateProfileString(strSection, strKey, Default, objResult, objResult.Capacity, strFilename);
if (intCharCount > 0)
functionReturnValue = Strings.Left(objResult.ToString(), intCharCount);
return functionReturnValue;
}
public string ReadINI(string Key, string Default)
{
string functionReturnValue = null;
// Returns a string from your INI file
int intCharCount = 0;
System.Text.StringBuilder objResult = new System.Text.StringBuilder(256);
if (string.IsNullOrEmpty(strSection)) {
MessageBox.Show("The INI File Class Does Not Have A Defined Section.", "INI Error");
return;
}
strKey = Key;
intCharCount = GetPrivateProfileString(strSection, Key, Default, objResult, objResult.Capacity, strFilename);
if (intCharCount > 0)
functionReturnValue = Strings.Left(objResult.ToString(), intCharCount);
return functionReturnValue;
}
public string ReadINI(string Section, string Key, string Default)
{
string functionReturnValue = null;
// Returns a string from your INI file
int intCharCount = 0;
System.Text.StringBuilder objResult = new System.Text.StringBuilder(256);
strSection = Section;
strKey = Key;
intCharCount = GetPrivateProfileString(Section, Key, Default, objResult, objResult.Capacity, strFilename);
if (intCharCount > 0)
functionReturnValue = Strings.Left(objResult.ToString(), intCharCount);
return functionReturnValue;
}
public string ReadINI(string Section, string Key, string Default, string sFileName)
{
string functionReturnValue = null;
// Returns a string from your INI file
int intCharCount = 0;
System.Text.StringBuilder objResult = new System.Text.StringBuilder(256);
strKey = Key;
strSection = Section;
strFilename = sFileName;
intCharCount = GetPrivateProfileString(Section, Key, Default, objResult, objResult.Capacity, sFileName);
if (intCharCount > 0)
functionReturnValue = Strings.Left(objResult.ToString(), intCharCount);
return functionReturnValue;
}
public void WriteINI(string Value)
{
// Writes a string to your INI file
string strMessage = null;
strMessage = "";
if (string.IsNullOrEmpty(strKey))
strMessage = "The INI File Class Does Not Have A Defined Key To Write.";
if (string.IsNullOrEmpty(strSection))
strMessage = strMessage + ControlChars.CrLf + "The INI File Class Does Not Have A Defined Section To Write.";
if (!string.IsNullOrEmpty(strMessage)) {
MessageBox.Show(strMessage, "INI Error");
return;
}
WritePrivateProfileString(strSection, strKey, Value, strFilename);
Flush();
}
public void WriteINI(string Key, string Value)
{
// Writes a string to your INI file
if (string.IsNullOrEmpty(strSection)) {
MessageBox.Show("The INI File Class Does Not Have A Defined Section To Write.", "INI Error");
return;
}
WritePrivateProfileString(strSection, Key, Value, strFilename);
Flush();
}
public void WriteINI(string Section, string Key, string Value)
{
// Writes a string to your INI file
strKey = Key;
strSection = Section;
WritePrivateProfileString(Section, Key, Value, strFilename);
Flush();
}
public void WriteINI(string Section, string Key, string Value, string sFileName)
{
strKey = Key;
strSection = Section;
strFilename = sFileName;
// Writes a string to your INI file
WritePrivateProfileString(Section, Key, Value, sFileName);
Flush();
}
private void Flush()
{
// Stores all the cached changes to your INI file
FlushPrivateProfileString(0, 0, 0, strFilename);
}
}
Ack, сделайте так, чтобы остановить, остановите! –
LOL, во всяком случае ... Возможно, мысль о том, чтобы использовать указатель вместо методов чтения/записи в вашем классе. Я сделал это на версии обертки INI, которую мне пришлось написать, и выглядел чистым и отлично работал. И хотя, вероятно, лучше избегать INI-файла, есть моменты, когда вам нужно это делать (например, взаимодействовать со старыми базовыми кодами). –
Этот вопрос немного неясно.Можете ли вы рассказать о части вопроса о имени и местоположении сервера MySQL? –
К сожалению, я имел в виду, хочу ли я поместить свое имя сервера MySQL в файл, который можно изменить вне моей программы. Например, если я хотел использовать другой сервер mysql для другого клиента, использующего программу. – muncherelli