简体   繁体   English

具有UnauthorizedAccessException的StreamWriter特权不足

[英]StreamWriter Insufficient Privileges With UnauthorizedAccessException

I am using the following code to write a file to the Desktop. 我正在使用以下代码将文件写入桌面。

string submittedFilePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
int i = 0;
StreamWriter sw = null;
sw = new StreamWriter(submittedFilePath, false);

for (i = 0; i < PSOLib.table.Columns.Count - 1; i++)
{
    sw.Write(PSOLib.table.Columns[i].ColumnName + ";");
}

sw.Write(PSOLib.table.Columns[i].ColumnName);
sw.WriteLine();

foreach (DataRow row in PSOLib.table.Rows)
{
    object[] array = row.ItemArray;

    for (i = 0; i < array.Length - 1; i++)
    {
        sw.Write(array[i].ToString() + ";");
    }

    sw.Write(array[i].ToString());
    sw.WriteLine();
}

sw.Close();

However whenever I try to invoke the method I get: 但是,每当我尝试调用该方法时,我都会得到:

Access to the path 'C:\\Users\\User\\Desktop' is denied. 
System.UnauthorizedAccessException.

You didn't specify a file for your StreamWriter , but a folder. 您没有为StreamWriter指定文件,而是为文件夹指定。

This should do it: 应该这样做:

string submittedFilePath = 
Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\myFile.txt.";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM