I am creating an appliaction in Windows 7. I use a SaveFileDialog to let the user select where to save files.
In windows 7 if you select a read only file and try to overwrite you get a popup saying you cant. You then have to choose another file to overwrite or just create a new save.
The problem im having is that in Windows XP you dont get the popup message saying you cant save. Instead the program throws an exception.
************** Exception Text **************
System.UnauthorizedAccessException: Access to the path 'C:\spanish.xml' is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
at System.Xml.XmlWriterSettings.CreateWriter(String outputFileName)
at System.Xml.XmlWriter.Create(String outputFileName, XmlWriterSettings settings)
at TOOL.XMLExporter.export(TabControl& languageTabs, ProgressBar& progressBar1, Int32 selectedLanguage)
at TOOL.Main.exportToXML()
at TOOL.Main.toxmlToolStripMenuItem_Click(Object sender, EventArgs e)
at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ToolStrip.WndProc(Message& m)
at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
This is the code I call:
SaveFileDialog fldg = new SaveFileDialog();
fldg.Title = "Save File";
fldg.InitialDirectory = @"c:\";
fldg.Filter = "(*.xml)|*.xml";
fldg.FilterIndex = 2;
fldg.RestoreDirectory = true;
string path = "";
if (fldg.ShowDialog() == DialogResult.OK)
{
path = fldg.FileName;
}
Is there a way to check if a file is read only and if so, display the same message(in xp).
Call SaveFileDialog again:
File is readonly ?