Friday, October 26, 2007

? Operator in C#

simple,

bool r = (somestring == "1" ? true : false);

Tuesday, October 23, 2007

How to write settings to App.config(Configuration in C# .NET 2.0)

System.Configuration.Configuration config =ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

config.AppSettings.Settings["IP"].Value = "3";
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");

yeap this is the microsoft example. doesn't work at all. !!! dont get caught this never works. I was trying hours and hours,

use this class .



public enum ConfigFileType
{
WebConfig,
AppConfig
}

public class AppConfig : System.Configuration.AppSettingsReader
{
public string docName = String.Empty;
private XmlNode node = null;

private int _configType;
public int ConfigType
{
get
{
return _configType;
}
set
{
_configType = value;
}
}

public bool SetValue(string key, string value)
{
XmlDocument cfgDoc = new XmlDocument();
loadConfigDoc(cfgDoc);
// retrieve the appSettings node
node = cfgDoc.SelectSingleNode("//appSettings");

if (node == null)
{
throw new System.InvalidOperationException("appSettings section not found");
}

try
{
// XPath select setting "add" element that contains this key
XmlElement addElem = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
if (addElem != null)
{
addElem.SetAttribute("value", value);
}
// not found, so we need to add the element, key and value
else
{
XmlElement entry = cfgDoc.CreateElement("add");
entry.SetAttribute("key", key);
entry.SetAttribute("value", value);
node.AppendChild(entry);
}
//save it
saveConfigDoc(cfgDoc, docName);
return true;
}
catch
{
return false;
}
}

private void saveConfigDoc(XmlDocument cfgDoc, string cfgDocPath)
{
try
{
XmlTextWriter writer = new XmlTextWriter(cfgDocPath, null);
writer.Formatting = Formatting.Indented;
cfgDoc.WriteTo(writer);
writer.Flush();
writer.Close();
return;
}
catch
{
throw;
}
}

public bool removeElement(string elementKey)
{
try
{
XmlDocument cfgDoc = new XmlDocument();
loadConfigDoc(cfgDoc);
// retrieve the appSettings node
node = cfgDoc.SelectSingleNode("//appSettings");
if (node == null)
{
throw new System.InvalidOperationException("appSettings section not found");
}
// XPath select setting "add" element that contains this key to remove
node.RemoveChild(node.SelectSingleNode("//add[@key='" + elementKey + "']"));

saveConfigDoc(cfgDoc, docName);
return true;
}
catch
{
return false;
}
}


private XmlDocument loadConfigDoc(XmlDocument cfgDoc)
{
// load the config file
if (Convert.ToInt32(ConfigType) == Convert.ToInt32(ConfigFileType.AppConfig))
{

docName = ((Assembly.GetEntryAssembly()).GetName()).Name;
docName += ".exe.config";
}
else
{
// docName = System.Web.HttpContext.Current.Server.MapPath("web.config");
}
cfgDoc.Load(docName);
return cfgDoc;
}

}


==========================

Eg

AppConfig a = new AppConfig();
a.ConfigType = 1;
a.SetValue("IP", "123132123123");

============================

Monday, October 22, 2007

weekend

It's been some time since my last post. Last weekend i spent most of the time watching One Tree Hill season 4. Nothing much interesting comparing to season 3 I guess. Brooke breaks up with Lucas, and move on with their life's.

Thursday, October 18, 2007

ClickOnce on Vista

I was assigned to check Richfocus on vista using ClickOnce installation. since we need administration rights on Vista pc to install keybordhooks, I deceied to use





inside the manifest.... after that lots of issues started

1. The requested operation requires elevation
2. Execution level requested by the application is not supported.

are two common error in this field.

When i googled it, it says due to some security issues, it's not allowed to have admin previlage user context inside.

only solution for this problem is to re-run the exe file using shell command. :)

hope you guys will not waste time on this.. !

Tuesday, October 16, 2007

Vista Crack

I wanted to install Vista 64 bit version on one of the testing PCs to test RichFocus. when i tried to apply the crack it fails. To make it work you have to disable "User Account Control (UAC)" which protects Registry, services ext,

Turn Off or Disable User Account Control (UAC) in Windows Vista

then you have to apply this crack :)

Unicode bug in RichTextBox

i found this today so i though of blogging it. there is a bug in the "_doc.Append" in the extended richtextbox. you have to replace that from this to work

replace:
_doc.Append(_text.Replace("\n", @"\par "));

with this:

string Text = _text.Replace("\n", @"\par ");
char[] chars = Text.ToCharArray();
for (int i = 0; i < chars.Length; i++)
{
if ((int)chars[i] > 256)
{
_doc.AppendFormat(@"\u" + ((int)chars[i]).ToString() + "?");
}
else
{
_doc.Append(chars[i].ToString());
}
}

Thursday, October 04, 2007

top most control in .NET

well in VB 6 we had zorder? in .net it's not there any more. if you want to set the top most control you have to use Controls.SetChildIndex(control, 0)

Tuesday, October 02, 2007

System.InvalidOperationException:

if you get this error you are screwed !!. it causes at no point! you just get the error from no where. solution is simple ... remove all Application.DoEvents() from your application

System.InvalidOperationException: The Undo operation encountered a context that is different from what was applied in the corresponding Set operation. The possible cause is that a context was Set on the thread and not reverted(undone).
at System.Threading.SynchronizationContextSwitcher.Undo()
at System.Threading.ExecutionContextSwitcher.Undo()
at System.Threading.ExecutionContext.runFinallyCode(Object userData, Boolean exceptionThrown)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteBackoutCodeHelper(Object backoutCode, Object userData, Boolean exceptionThrown)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Net.ContextAwareResult.Complete(IntPtr userToken)
at System.Net.LazyAsyncResult.ProtectedInvokeCallback(Object result, IntPtr userToken)
at System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)