-1
好吧,我已經看過與我的問題有關的其他每個問題,但無法看到我出錯的地方。我的目標是將骰子模擬器的結果寫入文本文件,然後將這些內容讀入C#中的列表框。這是我到目前爲止的代碼:閱讀和寫入C文本文件的問題#
namespace WpfApplication1
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
try
{
Random RandomValue = new Random();
Random RandomValue2 = new Random();
using (StreamWriter outputFile = new StreamWriter("C:\\Example.txt"))
{
for (int i = 0; i < 100; i++)
{
int face1 = RandomValue.Next(1, 7);
int face2 = RandomValue2.Next(1, 7);
int toss = face1 + face2;
outputFile.WriteLine(toss);
}
outputFile.Close();
}
}
catch (Exception ex)
{
listBox1.Items.Add(ex);
}
}
private void button2_Click(object sender, RoutedEventArgs e)
{
try
{
using (StreamReader inputFile = new StreamReader("C:\\Example.txt"))
{
String toss;
for (int i = 0; i > 100; i++)
{
toss = inputFile.ReadLine();
listBox1.Items.Add(toss);
}
catch (Exception ex)
{
listBox1.Items.Add(ex);
}
}
}
}
}
當我嘗試運行程序我收到錯誤:System.UnauthorizedAccessException: Access to the path 'C:\Example.txt' is denied.
我覺得我要去的地方錯了是實際創建的文本文件。當我嘗試從它讀取時,我收到相同的消息。我對C#非常陌生,並且覺得我沒有正確調用文件。任何見解都將非常受歡迎!
嘗試以* Administrator *身份運行Visual Studio。 – 2014-09-21 18:43:25
使用不需要提升特權的目錄,例如'Path.GetTempPath()' – 2014-09-21 19:15:33
爲了澄清,如果UAC處於打開狀態,C:驅動器的根限制爲停止應用程序在那裏轉儲內容。 D:分區的根目錄可以工作,但最佳實踐是用戶帳戶區域內的目錄。 – WhoIsRich 2014-09-21 19:28:00