file exists method with programfiles folder

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have a c# 2.5 framework desktop app. that displays a welcome form only when the app is first run. Works properly if the app is run as administrator or if the user has admin rights, even if the programfiles folder is readonly. Otherwise it is
not shown when first run. The IsFirstTime flag controls showing the welcome screen and is set to true and saved to a config file after the welcome screen is shown. So why doesnt the welcome screen show when app is not run as administrator. I notice that programfiles
folder is readonly so that should result in the welcome screen showing everytime the app is run, but it never shows. testing so far on two Windows 7 machines.
<br/>
private bool IsFirstTime = true;
private void Form1_Load(object sender, EventArgs e)<br/>
{<br/>
ReadConfigFile();<br/>
if (IsFirstTime == true)<br/>
{<br/>
Intro welcome = new Intro();<br/>
welcome.ShowDialog();<br/>
//set IsFirstTime false now that into has been shown, then save that value to config file<br/>
IsFirstTime = false;<br/>
SaveConfigFile();<br/>
}
}<br/>
private void ReadConfigFile()<br/>
{<br/>
string file = Application.StartupPath + " file://\XmlmaxViewerConfig.cfg \XmlmaxViewerConfig.cfg ";<br/>
if (File.Exists(file))<br/>
{<br/>
try<br/>
{<br/>
string elementName = string.Empty;<br/>
XmlReaderSettings settings = new XmlReaderSettings();<br/>
settings.ProhibitDtd = true; //want error if found
<br/>
XmlReader reader = XmlReader.Create(file, settings);<br/>
while (reader.EOF == false)<br/>
{<br/>
reader.Read();<br/>
if (reader.Depth == 0) continue;<br/>
if (reader.NodeType == XmlNodeType.Element)<br/>
{<br/>
elementName = reader.Name;<br/>
}<br/>
else if (reader.NodeType == XmlNodeType.Text)<br/>
{<br/>
switch (elementName)<br/>
{
<br/>
case "FirstTime":<br/>
IsFirstTime = reader.Value == "True" ? true :
false;<br/>
break;<br/>
case "FormWidth":<br/>
formWidth = Convert.ToInt32(reader.Value);<br/>
break;<br/>
case "FormHeight":<br/>
formHeight = Convert.ToInt32(reader.Value);<br/>
break;<br/>
}<br/>
}<br/>
}<br/>
reader.Close();<br/>
}
catch (Exception)<br/>
{<br/>
//MessageBox.Show("Error reading configuration file " + file + Environment.NewLine<br/>
// + ex.Message + Environment.NewLine + file);<br/>
//return;<br/>
}<br/>
}<br/>
}
internal static void SaveConfigFile()<br/>
{<br/>
//called by form1_load only if firstTime is true. also called when user exits program -must be called before call to application.exit or the width and heights will be null.<br/>
string file = Application.StartupPath + " file://\XmlmaxViewerConfig.cfg \XmlmaxViewerConfig.cfg ";<br/>
XmlWriterSettings settings = new XmlWriterSettings();<br/>
try<br/>
{<br/>
using (XmlWriter writer = XmlWriter.Create(file, settings))<br/>
{<br/>
// Write XML data.<br/>
//start w/newline: xmlwriter automatically emits an xmldeclaration, so want new line after it<br/>
writer.WriteWhitespace(Environment.NewLine);<br/>
writer.WriteStartElement("Options");<br/>
writer.WriteWhitespace(Environment.NewLine);<br/>
writer.WriteElementString("FirstTime", "False");<br/>
writer.WriteWhitespace(Environment.NewLine);<br/>
writer.WriteElementString("FormWidth", Application.OpenForms[0].Width.ToString());<br/>
writer.WriteWhitespace(Environment.NewLine);<br/>
writer.WriteElementString("FormHeight", Application.OpenForms[0].Height.ToString());<br/>
writer.WriteWhitespace(Environment.NewLine);<br/>
writer.WriteEndElement();<br/>
writer.Flush();<br/>
}<br/>
}<br/>
catch (Exception){ }
}

View the full article
 
Back
Top