I want to get export from datagridview to text file but I get error
<p>I want to get export from datagridview to text file but i get following <code>error</code> : </p>

<pre><code>An unhandled exception of type 'System.Security.SecurityException' 
occurred in mscorlib.dll

Additional information: Request for the permission of type
'System.Security.Permissions.FileIOPermission, mscorlib,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
</code></pre>

<p>This is my <code>code</code> :</p>

<pre><code>const string path = @"d:\export.txt";
if (!File.Exists(path))
{
    File.Create(path);
}
TextWriter sw = new StreamWriter(@"d:\export.txt");

int rowcount = dgvSum.Rows.Count;
for (int i = 0; i &lt; rowcount - 1; i++)
{
    sw.WriteLine(dgvSum.Rows[i].Cells[0].Value.ToString());
}
sw.Close();


MessageBox.Show(@"Text file was created.");
</code></pre>

<p>this is my report for try-catch :
<a href="https://i.stack.imgur.com/Tfyrv.jpg" rel="nofollow noreferrer">this is my report for try-catch:</a></p>

<p><a href="https://i.stack.imgur.com/HOKvf.jpg" rel="nofollow noreferrer">this is report after changeing path and filename</a></p>

<p>This id exact <code>code</code> after some edit :</p>

<pre><code>try
{
    const string path = @"c:\123\123.txt";

    using (FileStream fileStream = File.Open(path, 
    FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
    using (TextWriter sw = new StreamWriter(fileStream))

    {
        int rowcount = dgvSum.Rows.Count;
        for (int i = 0; i &lt; rowcount - 1; i++)
        {
            sw.WriteLine(dgvSum.Rows[i].Cells[0].Value.ToString());
        }
    }
    MessageBox.Show(@"Text file was created.");
}
catch (Exception exception)
{
    MessageBox.Show(exception.ToString());
    //Console.WriteLine(exception);
}
</code></pre>