如何解决此错误“尝试读取或写入受保护的内存”。这往往表明其他记忆已经腐败。“ [英] How Do I Solve This Error"Attempted To Read Or Write Protected Memory. This Is Often An Indication That Other Memory Is Corrupt."

查看:68
本文介绍了如何解决此错误“尝试读取或写入受保护的内存”。这往往表明其他记忆已经腐败。“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何解决此错误尝试读取或写入受保护的内存。这通常表示其他内存已损坏。

at line(... if(savefiledialog1.ShowDialog( )== DialogResult.OK)

...)



这是我的代码



How do i solve this Error"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
at line (...if (savefiledialog1.ShowDialog() == DialogResult.OK)
...)

Here is my code

  private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string folderPath = string.Empty;
            SaveFileDialog savefiledialog1 = new SaveFileDialog();
            savefiledialog1.InitialDirectory = @"C:\";
            savefiledialog1.Filter = "Xml files (*.xml)|*.xml|All files (*.*)|*.*";
            savefiledialog1.RestoreDirectory = true;
/////////////////////////////////////
            if (savefiledialog1.ShowDialog() == DialogResult.OK)//Error is ///coming in this line
            {
                
                    folderPath = savefiledialog1.FileName;
                    if (!System.IO.File.Exists(folderPath))
                    {
                        using (XmlWriter writer = XmlWriter.Create(folderPath))
                        {
                        }
                    }
            }
        }

< br $>




请帮帮我...



thankyou




pls help me...

thankyou

推荐答案

您在问题中显示的任何代码行都不会发生。在我对这个问题的评论中,我试图解释原因。找到异常的来源并返回。您可以捕获每个线程的顶层堆栈帧上的所有异常,在调试器下捕获并查看调试窗口调用堆栈。它会告诉你它来自何处。



顺便说一句,不要将对话框初始目录设置为C:。这不是一个好的初始目录,特别是对于写入。更重要的是,对于您的信息,并非所有目标系统都具有此磁盘卷。最好保留这个目录。首次运行后,将按用户帐户记住每个用户使用的上一个目录。 (这一段与问题无关;只是一个建议。)



-SA
It cannot happen any any of the lines of code you've shown in your question. In my comment to the question, I tried to explain why. Locate the source of exception and come back. You can catch all exceptions on top stack frame of each thread, catch in under the debugger and look at the debug window "Call stack". It will show you where it comes from.

By the way, don't set dialog initial directory to "C:". This is not a good initial directory, especially for writing. More importantly, for your information, not all target systems will even have this disk volume. Better leave this directory as is. After first run, the previous directory used by each user will be remembered, per user account. (This paragraph is unrelated to the question; just an advice.)

—SA


这篇关于如何解决此错误“尝试读取或写入受保护的内存”。这往往表明其他记忆已经腐败。“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆