Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SaveFileDialog.net--覆盖只读文件_C#_Windows 7_Windows Xp_Savefiledialog - Fatal编程技术网

C# SaveFileDialog.net--覆盖只读文件

C# SaveFileDialog.net--覆盖只读文件,c#,windows-7,windows-xp,savefiledialog,C#,Windows 7,Windows Xp,Savefiledialog,我正在Windows7中创建一个应用程序。我使用SaveFileDialog让用户选择保存文件的位置 在Windows7中,如果您选择一个只读文件并试图覆盖,则会出现一个弹出窗口,提示您不能。然后,您必须选择另一个要覆盖的文件,或者只是创建一个新的保存 我遇到的问题是,在Windows XP中,你不会收到弹出消息说你无法保存。相反,程序会抛出一个异常 ************** Exception Text ************** System.UnauthorizedAccessExc

我正在Windows7中创建一个应用程序。我使用SaveFileDialog让用户选择保存文件的位置

在Windows7中,如果您选择一个只读文件并试图覆盖,则会出现一个弹出窗口,提示您不能。然后,您必须选择另一个要覆盖的文件,或者只是创建一个新的保存

我遇到的问题是,在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中)。

再次调用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)
{
   ...
}

再次调用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)
{
   ...
}

您的预期行为是什么?@okrumnow“是否有办法检查文件是否为只读,如果是,则显示相同的消息(在xp中)。”以及您的预期行为是什么?@okrumnow“是否有办法检查文件是否为只读,如果是,则显示相同的消息(在xp中)。“如果我正在创建新文件,这是否也有效?”?我认为文件名是存在的。如果它不存在怎么办?如果它不存在,.IsReadOnly返回true。(您也可以使用.Exists进行检查)另一个问题是获取需要关闭对话框的“文件名”。但它从来没有这样做过,因为以前抛出过异常。如果我正在创建一个新文件,这会起作用吗?我认为文件名是存在的。如果它不存在怎么办?如果它不存在,.IsReadOnly返回true。(您也可以使用.Exists进行检查)另一个问题是获取需要关闭对话框的“文件名”。但它从来不会这样做,因为以前抛出过异常。