SaveFileDialog .net -- 覆盖只读文件

发布于 2025-01-02 02:21:22 字数 2879 浏览 3 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜夜流光相皎洁 2025-01-09 02:21:22

再次调用 SaveFileDialog:

bool foundFile = false;
SaveFileDialog dlg = new SaveFileDialog();

while (!foundFile)
{
     if (dlg.ShowDialog() != DialogResult.OK)
        break;

     FileInfo info = new FileInfo(dlg.FileName);
     if (!info.IsReadOnly)
     {
          MessageBox.Show("File is readonly.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
     else
     {
          foundFile = true;
     }
}      

文件是只读的?

FileInfo info = new FileInfo(filename);
if(info.IsReadOnly)
{
   ...
}

Call SaveFileDialog again:

bool foundFile = false;
SaveFileDialog dlg = new SaveFileDialog();

while (!foundFile)
{
     if (dlg.ShowDialog() != DialogResult.OK)
        break;

     FileInfo info = new FileInfo(dlg.FileName);
     if (!info.IsReadOnly)
     {
          MessageBox.Show("File is readonly.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
     else
     {
          foundFile = true;
     }
}      

File is readonly ?

FileInfo info = new FileInfo(filename);
if(info.IsReadOnly)
{
   ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文