HELP Erro .NET CSharp - System.UnauthorizedAccessException

04/07/2016

0

Ola, estou fazendo o curso "Introdução do .NET Framework", na aula 12 "System.Xml"
Fiz o código da aula, o problema aparece quando vou criar o arquivo XML, da acesso negado a pasta. Como faço pra resolver ? Já tentei alterar a permissão desta pasta mas não resolveu.
Estou usando o Windows 10 Home 64bits, Visual Studio 2015 Community e .NET 4.5

{
    public partial class Form1 : Form
    {
        string arquivo = @"C:\...";
        XmlDocument xmlDoc = new XmlDocument();

        public Form1()
        { 
            InitializeComponent();

            if (!File.Exists(arquivo))
            {
                InitializeComponent();
                XmlNode noteRoot = xmlDoc.CreateElement("Contato");
                xmlDoc.AppendChild(noteRoot);
                xmlDoc.Save(arquivo);
            }
        }

        private void btnSalvar_Click(object sender, EventArgs e)
        {
            xmlDoc.Load(arquivo);
            XmlNode nodeNome = xmlDoc.CreateElement("Nome");
            XmlNode nodeTelefone = xmlDoc.CreateElement("Telefone");
            nodeNome.InnerText = txtNome.Text;
            nodeTelefone.InnerText = txtTelefone.Text;
            xmlDoc.SelectSingleNode("/Contato").AppendChild(nodeNome);
            xmlDoc.SelectSingleNode("/Contato").AppendChild(nodeTelefone);
            xmlDoc.Save(arquivo);
        }
    }
}


-------------------------------------------

DETALHES DO ERRO:

Consulte o final desta mensagem para obter detalhes sobre como chamar a
depuração just-in-time (JIT) em vez desta caixa de diálogo.

************** Texto de Exceção **************
System.InvalidOperationException: Este documento já contém um nó 'DocumentElement'.
em System.Xml.XmlDocument.IsValidChildType(XmlNodeType type)
em System.Xml.XmlNode.AppendChild(XmlNode newChild)
em System.XML_Example.Form1.btnCriarArquivo_Click(Object sender, EventArgs e) na C:\Users\guisfits\Documents\Visual Studio 2015\Projects\Introdução ao .NET Framework\System.XML_Example\Form1.cs:linha 44
em System.Windows.Forms.Control.OnClick(EventArgs e)
em System.Windows.Forms.Button.OnClick(EventArgs e)
em System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
em System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
em System.Windows.Forms.Control.WndProc(Message& m)
em System.Windows.Forms.ButtonBase.WndProc(Message& m)
em System.Windows.Forms.Button.WndProc(Message& m)
em System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
em System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
em System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Assemblies Carregados **************
mscorlib
Versão do Assembly: 4.0.0.0
Versão do Win32: 4.6.1080.0 built by: NETFXREL3STAGE
Base de Código: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
System.XML_Example
Versão do Assembly: 1.0.0.0
Versão do Win32: 1.0.0.0
Base de Código: file:///C:/Users/guisfits/Documents/Visual%20Studio%202015/Projects/Introdu%C3%A7%C3%A3o%20ao%20.NET%20Framework/System.XML_Example/bin/Debug/System.XML_Example.exe
----------------------------------------
System.Windows.Forms
Versão do Assembly: 4.0.0.0
Versão do Win32: 4.6.1038.0 built by: NETFXREL2
Base de Código: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
Versão do Assembly: 4.0.0.0
Versão do Win32: 4.6.1081.0 built by: NETFXREL3STAGE
Base de Código: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
Versão do Assembly: 4.0.0.0
Versão do Win32: 4.6.1078.0 built by: NETFXREL3STAGE
Base de Código: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Xml
Versão do Assembly: 4.0.0.0
Versão do Win32: 4.6.1064.2 built by: NETFXREL3STAGE
Base de Código: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.Configuration
Versão do Assembly: 4.0.0.0
Versão do Win32: 4.6.1038.0 built by: NETFXREL2
Base de Código: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Core
Versão do Assembly: 4.0.0.0
Versão do Win32: 4.6.1038.0 built by: NETFXREL2
Base de Código: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------
mscorlib.resources
Versão do Assembly: 4.0.0.0
Versão do Win32: 4.6.1038.0 built by: NETFXREL2
Base de Código: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/mscorlib.resources/v4.0_4.0.0.0_pt-BR_b77a5c561934e089/mscorlib.resources.dll
----------------------------------------
System.Windows.Forms.resources
Versão do Assembly: 4.0.0.0
Versão do Win32: 4.6.1038.0 built by: NETFXREL2
Base de Código: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms.resources/v4.0_4.0.0.0_pt-BR_b77a5c561934e089/System.Windows.Forms.resources.dll
----------------------------------------
System.xml.resources
Versão do Assembly: 4.0.0.0
Versão do Win32: 4.6.1038.0 built by: NETFXREL2
Base de Código: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Xml.resources/v4.0_4.0.0.0_pt-BR_b77a5c561934e089/System.Xml.resources.dll
----------------------------------------

************** Depuração JIT **************
Para habilitar a depuração just-in-time (JIT), o arquivo .config deste
aplicativo ou computador (machine.config) deve ter o valor
jitDebugging definido na seção system.windows.forms.
O aplicativo também deve ser compilado com a depuração
habilitada.

Por exemplo:

<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>

Quando a depuração JIT está habilitada, qualquer exceção sem tratamento
será enviada ao depurador JIT registrado no computador,
em vez de ser tratada nesta caixa de diálogo.
Guilherme Silva

Guilherme Silva

Responder

Post mais votado

13/07/2016

no "C:\" é complicado, tenta gravar em outra pasta para fazer teste "c:\teste\", se quiser gravar no "C:\" tenta executar como Admin o programa.

Lucas Ramos

Lucas Ramos
Responder

Assista grátis a nossa aula inaugural

Assitir aula

Saiba por que programar é uma questão de
sobrevivência e como aprender sem riscos

Assistir agora

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar