Beloew是我的代码,我想保存一些信息,用户密钥保存到文件中,为什么我在调试后点击保存按钮后仍然遇到Ioexception错误..请点赞adv..thks! !我想这与文件夹有关,但绝对不知道..
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace ASSISNMENTTT
{
public partial class Registeration : Form
{
public Registeration()
{
InitializeComponent();
}
private void Btn_Save_Click(object sender, EventArgs e)
{
// This is the button labeled "Save" in the program.
//
File.WriteAllText("C:\\demo.txt", Tb_Admin.Text);
File.WriteAllText("C:\\demo.txt", Tb_Name.Text);
File.WriteAllText("C:\\demo.txt", Tb_Gender.Text);
}
}
}
答案 0 :(得分:0)
您很可能使用的是Win7或Vista,并且无法访问c:\驱动器。
答案 1 :(得分:0)
如果您的应用确实可以访问C:\驱动器,则demo.txt可能标记为只读,因为WriteAllText
会尝试覆盖该文件。
此代码显示UnauthorizedAccessException,其错误文本与您对问题和其他答案的评论相同。
static void Main(string[] args)
{
var demoTxt = new FileInfo("C:\\demo.txt");
demoTxt.Attributes |= FileAttributes.ReadOnly;
WriteAllText("should succeed");
try
{
demoTxt.Attributes |= FileAttributes.ReadOnly;
WriteAllText("should fail");
}
catch (UnauthorizedAccessException uae)
{
Debug.WriteLine(uae.ToString());
}
}
static void WriteAllText(string text)
{
// This is the button labeled "Save" in the program.
//
File.WriteAllText("C:\\demo.txt", text);
}
为了将来参考,这可以帮助您提供有关SO帖子的更多信息。
private void Btn_Save_Click(object sender, EventArgs e)
{
try
{
// This is the button labeled "Save" in the program.
//
File.WriteAllText("C:\\demo.txt", Tb_Admin.Text);
File.WriteAllText("C:\\demo.txt", Tb_Name.Text);
File.WriteAllText("C:\\demo.txt", Tb_Gender.Text);
}
catch(IoException ex)
{
//View the Output Window, copy the text to your question
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}