I want to get export from datagridview to text file but I get error

一个人想着一个人 提交于 2020-01-06 20:02:20

问题


I want to get export from datagridview to text file but i get following error :

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.

This is my 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 < rowcount - 1; i++)
{
    sw.WriteLine(dgvSum.Rows[i].Cells[0].Value.ToString());
}
sw.Close();


MessageBox.Show(@"Text file was created.");

this is my report for try-catch : this is my report for try-catch:

this is report after changeing path and filename

This id exact code after some edit :

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 < 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);
}

回答1:


The reason of System.Security.SecurityException in your call of File.Create method. It creates file and opens FileStream on created file. You did not close opened by File.Create stream so StreamWriter can not open a second one.

Change code to following:

const string path = @"d:\export.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 < rowcount - 1; i++) {
        sw.WriteLine(dgvSum.Rows[i].Cells[0].Value.ToString());
    }
}
MessageBox.Show(@"Text file was created.");



回答2:


Try this code

try
{
    string filepath = @"d:\export.txt"
    using (TextWriter stream = File.Exists(filepath) ? new StreamWriter(filepath) : new StreamWriter(File.Create(filepath)))
    {
        int rowcount = dgvSum.Rows.Count;
        for(int i = 0; i < rowcount - 1; i++) 
        {
            stream.WriteLine(dgvSum.Rows[i].Cells[0].Value.ToString());
        }
    }
}
catch (Exception) { } 



回答3:


  1. right click on the file "export.txt" -> Properties -> Security make sure your user has write permission.
  2. use streams inside using statement like

    using (var fileStream = new FileStream(file, FileMode.Open))
    {
         using (var textReader = new StreamReader(fileStream))
         {
         }
     }
    



回答4:


add this in config file and I think this will work for you



来源:https://stackoverflow.com/questions/37676746/i-want-to-get-export-from-datagridview-to-text-file-but-i-get-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!