1

i am reading a strvalue from .ini file. Its reading and every thing is working fine. But i wanted to show something like when the keyname and strvale is not there i want to show a message box like "contact admin".

So when i coded on the form_load even.. each time the form is loading with the message box. lol. i dont khow to correct it. Please help me out.

My code snippet

 private void Form1_Load(object sender, EventArgs e)
    {
        if (!System.IO.File.Exists(ini.ReadValue("Action", "User")))
        {
            MessageBox.Show("Contact Admin");
            Environment.Exit(0); 
        }
       else
        { 
        //do something
        }
   }

Code in my .ini file

  [Action] 
  User=Company\Group

I am looking for something like if the .ini [Action] filed is empty, means like below!!!

[Action]
User=

So i need to show an message box on form load and i want to exit.

I have made a seperate class to read and write .ini file. Code snippet for .ini file

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace MovedFiles
{
class INIFile
{
    [DllImport("kernel32.dll")]
    private static extern int WritePrivateProfileString(string ApplicationName, string KeyName, string StrValue, string FileName);
    [DllImport("kernel32.dll")]
    private static extern int GetPrivateProfileString(string ApplicationName, string KeyName, string DefaultValue, StringBuilder ReturnString, int nSize, string FileName);

    private string fileName = string.Empty;
    public INIFile(string _fileName)
    {
        fileName = _fileName;
    }
    private string DefaultSection = "SECTION1";
    public INIFile(string _fileName, string _defaultSection)
    {
        fileName = _fileName;
        DefaultSection = _defaultSection;
    }

    public void WriteValue(string SectionName, string KeyName, string KeyValue)
    {
        WritePrivateProfileString(SectionName, KeyName, KeyValue, fileName);
    }

    public string ReadValue(string SectionName, string KeyName)
    {
        StringBuilder szStr = new StringBuilder(255);
        GetPrivateProfileString(SectionName, KeyName, "", szStr, 255, fileName);
        return szStr.ToString().Trim();
    }

    public void WriteValue(string KeyName, string KeyValue)
    {
        WriteValue(DefaultSection, KeyName, KeyValue);
    }
    public string ReadValue(string KeyName)
    {
        return ReadValue(DefaultSection, KeyName);
    }
}

}

3
  • 3
    you are reading the wrong code
    – Milan phir
    Sep 12, 2014 at 2:29
  • 1
    No.. Who need to do like that!!! I need to show the message box!! if the .ini file's Action filed.. is empty.. Just to show a message "contact admin" if filed is emplty:p
    – user4009512
    Sep 12, 2014 at 2:32
  • Don't use an ini file. Use an xml file and serialize/deserialize the entire setting in 3 lines or so.. Sep 12, 2014 at 5:07

2 Answers 2

2

One issue is that you are using System.IO.File.Exists() on something that is not a file path. I don't know what library you are using to read the ini file, I imagine it is just wrapping the Win32 calls. You may be looking for something like:

if(!string.IsNullOrEmpty(ini.ReadValue("Action", "User")))
{
    MessageBox.Show("Contact Admin.");
}

This assumes you have specified the file and checked that it exists when creating the ini variable.

8
  • 1
    @BobbyShaftoe. I think File.Exists should return false for both null or empty.
    – Hassan
    Sep 12, 2014 at 2:39
  • Ohhh sorry its not working.. its same for both case.. i gave the path there.. still its showing contact admin
    – user4009512
    Sep 12, 2014 at 2:45
  • 1
    @StacyKebler, post ini.ReadValue code in your question.
    – Hassan
    Sep 12, 2014 at 2:47
  • 1
    @StacyKebler. Answer is correct. You can have values in string sUserVal = ini.ReadValue("Action", "User"); then check it with string.IsNullOrEmpty
    – Hassan
    Sep 12, 2014 at 2:58
  • 1
    @HassanNisar, I agree that File.Exists returns false in this case but really it will always return false given that the result of ReadValue, as I can tell, in this case, will never be a valid file path but rather some kind of user designation. Sep 12, 2014 at 3:14
0

You should check like this. you are checking File is exist or not instead of value is null or not.

string strVal = ini.ReadValue("Action", "User");
if(string.IsNullOrEmpty(strVal))  //This will return true if the value is null or blank.
{
    MessageBox.Show("Contact Admin."); //So the message will display only when value is not assinged.
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.