SaveFileDialog .net -- 覆盖只读文件
我正在 Windows 7 中创建一个应用程序。我使用 SaveFileDialog 让用户选择保存文件的位置。
在 Windows 7 中,如果您选择只读文件并尝试覆盖,您会收到一个弹出窗口,提示您不能覆盖。然后,您必须选择另一个文件来覆盖或只是创建一个新的保存。
我遇到的问题是,在 Windows XP 中,您不会收到表示无法保存的弹出消息。相反,程序会抛出异常。
************** 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)
这是我调用的代码:
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;
}
有没有办法检查文件是否只读,如果是,则显示相同的消息(在 xp 中)。
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
再次调用 SaveFileDialog:
文件是只读的?
Call SaveFileDialog again:
File is readonly ?