Click here to Skip to main content
15,907,395 members
Home / Discussions / C#
   

C#

 
JokeRe: VERY URGENT!!!!!! PinPopular
musefan2-Jun-09 3:03
musefan2-Jun-09 3:03 
GeneralRe: VERY URGENT!!!!!! Pin
0x3c02-Jun-09 3:08
0x3c02-Jun-09 3:08 
JokeRe: VERY URGENT!!!!!! Pin
musefan2-Jun-09 3:11
musefan2-Jun-09 3:11 
GeneralRe: VERY URGENT!!!!!! Pin
Henry Minute2-Jun-09 3:40
Henry Minute2-Jun-09 3:40 
GeneralWrong Forum Pin
dan!sh 2-Jun-09 4:10
professional dan!sh 2-Jun-09 4:10 
GeneralRe: Wrong Forum Pin
musefan2-Jun-09 4:18
musefan2-Jun-09 4:18 
JokeRe: VERY URGENT!!!!!! Pin
Wes Aday2-Jun-09 4:17
professionalWes Aday2-Jun-09 4:17 
QuestionC# Configuration Files Pin
staticv2-Jun-09 2:41
staticv2-Jun-09 2:41 
I am encountering a problem while reading config files. The scenario is like this, I have two programs. Program A reads its configuration from a config file and program B is only used to modify the contents of the config file which A reads. The name of the config file is email.config. It is in the same directory in which program A & B resides.

The problem is that I get path of a file for attachment using open file dialog in program B. If the path points to a file in the same directory, program B works perfect while saving the data back to the config file! But if it points to a file outside the directory it throws an exception of type System.NullReferenceException.

I have placed comments in the code where the exception occurs.

Here is the code

static readonly string configFileName = "email.config";

private void saveBtn_Click(object sender, EventArgs e)
{
	try
	{
		// save everything and close
		string serverAddr = serverAddressTxtBox.Text;
		string port = portTxtBox.Text;
		bool ssl = sslRadioYes.Checked == true ? true : false;
		string senderAddr = senderTxtBox.Text;
		string password = passwordTxtBox.Text.Length != 0 ? Encrypt(passwordTxtBox.Text) : "";
		string subject = subjectTxtBox.Text;
		string attachment = attachTxtBox.Text;
		string messageBody = msgBodyTxtBox.Text;
		var configMap = new ExeConfigurationFileMap { ExeConfigFilename = configFileName };
		Configuration externalConfig = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
			
		/* If attachment points to a file outside the exes directory
		externalConfig doesn't reads the config properly
		and throws exception of type System.NullReferenceException */
		
		// here the exception is thrown
		externalConfig.AppSettings.Settings["ServerAddress"].Value = serverAddr;
		externalConfig.AppSettings.Settings["Port"].Value = port;
		externalConfig.AppSettings.Settings["SSL"].Value = ssl.ToString();
		externalConfig.AppSettings.Settings["Sender"].Value = senderAddr;
		externalConfig.AppSettings.Settings["SenderPassword"].Value = password;
		externalConfig.AppSettings.Settings["Subject"].Value = subject;
		externalConfig.AppSettings.Settings["AttachmentPath"].Value = attachment;
		externalConfig.AppSettings.Settings["Body"].Value = messageBody;

			// Save values in config
		externalConfig.Save(ConfigurationSaveMode.Full);
		Application.Exit();
	}
	catch (System.Exception ex)
	{
		MessageBox.Show("Error: " + ex.Message);
		Application.Exit();
	}
}


Top Web Hosting Providers[^]

Do, or do not. There is no 'try'.

AnswerRe: C# Configuration Files Pin
0x3c02-Jun-09 3:02
0x3c02-Jun-09 3:02 
GeneralRe: C# Configuration Files Pin
staticv2-Jun-09 3:05
staticv2-Jun-09 3:05 
AnswerRe: C# Configuration Files Pin
staticv2-Jun-09 4:59
staticv2-Jun-09 4:59 
QuestionHow I can update Child Table using Entity Framework (ugent) Pin
Qasim19842-Jun-09 1:57
professionalQasim19842-Jun-09 1:57 
AnswerRe: How I can update Child Table using Entity Framework (ugent) Pin
Ennis Ray Lynch, Jr.2-Jun-09 2:00
Ennis Ray Lynch, Jr.2-Jun-09 2:00 
GeneralRe: How I can update Child Table using Entity Framework (ugent) Pin
musefan2-Jun-09 2:27
musefan2-Jun-09 2:27 
GeneralRe: How I can update Child Table using Entity Framework (ugent) Pin
Pete O'Hanlon2-Jun-09 2:29
mvePete O'Hanlon2-Jun-09 2:29 
GeneralRe: How I can update Child Table using Entity Framework (ugent) Pin
musefan2-Jun-09 2:36
musefan2-Jun-09 2:36 
AnswerRe: How I can update Child Table using Entity Framework (ugent) Pin
Pete O'Hanlon2-Jun-09 2:17
mvePete O'Hanlon2-Jun-09 2:17 
GeneralRe: How I can update Child Table using Entity Framework (ugent) Pin
Qasim19842-Jun-09 2:21
professionalQasim19842-Jun-09 2:21 
GeneralRe: How I can update Child Table using Entity Framework (ugent) Pin
Colin Angus Mackay2-Jun-09 2:24
Colin Angus Mackay2-Jun-09 2:24 
GeneralRe: How I can update Child Table using Entity Framework (ugent) Pin
Rajesh R Subramanian2-Jun-09 2:26
professionalRajesh R Subramanian2-Jun-09 2:26 
GeneralRe: How I can update Child Table using Entity Framework (ugent) Pin
Pete O'Hanlon2-Jun-09 2:29
mvePete O'Hanlon2-Jun-09 2:29 
AnswerRe: How I can update Child Table using Entity Framework (ugent) Pin
0x3c02-Jun-09 2:30
0x3c02-Jun-09 2:30 
GeneralRe: How I can update Child Table using Entity Framework (ugent) Pin
Qasim19842-Jun-09 4:57
professionalQasim19842-Jun-09 4:57 
JokeRe: How I can update Child Table using Entity Framework (ugent) Pin
musefan2-Jun-09 2:34
musefan2-Jun-09 2:34 
GeneralRe: How I can update Child Table using Entity Framework (ugent) Pin
EliottA2-Jun-09 2:51
EliottA2-Jun-09 2:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.