權限有哪些是應該打開文件,然後輸出到控制檯添加行號的小程序。問題是無論程序是否從IDE的命令控制檯運行,它都會拋出有關文件權限的異常。Visual Studio 2010和中的文件
我將可執行文件和應該讀取的文件(簡單的TXT文件)移動到多個目錄(我的文檔,臨時文件等),以管理員身份運行控制檯,以管理員身份運行Visual Studio,同時爲這兩個文件,但它總是拋出異常。最奇怪的是,一兩週前我通過反覆試驗來解決問題,但我可以'記住它。
這裏是例外:
Exception: System.UnauthorizedAccessException: Access to the path 'C:\Users\Nena
d\documents\visual studio 2010\Projects\Listing 10.6\Listing 10.6\bin\Debug\prog
ram.cs' is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, I
nt32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions o
ptions, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolea
n useLongPath)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access,
FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean
bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode)
at ListFile.Main(String[] args) in C:\Users\Nenad\documents\visual studio 201
0\Projects\Listing 10.6\Listing 10.6\Program.cs:line 22
Press any key to continue . . .
這裏是代碼:
// ListFile.cs - program to print a listing to the console
//-----------------------------------------------------------
using System;
using System.IO;
class ListFile
{
public static void Main(string[] args)
{
try
{
int ctr = 0;
if (args.Length <= 0)
{
Console.WriteLine("Format: ListFile filename");
return;
}
else
{
FileStream fstr = new FileStream(args[0], FileMode.Open);
try
{
StreamReader t = new StreamReader(fstr);
string line;
while ((line = t.ReadLine()) != null)
{
ctr++;
Console.WriteLine("{0}: {1}", ctr, line);
}
}
catch (Exception e)
{
Console.WriteLine("Exception during read/write: {0}\n", e);
}
finally
{
fstr.Close();
}
}
}
catch (System.IO.FileNotFoundException)
{
Console.WriteLine("ListFile could not find the file {0}", args[0]);
}
catch (Exception e)
{
Console.WriteLine("Exception: {0}\n\n", e);
}
}
}
在windows -explorer導航到您的解決方案所在的文件夾。並取消只讀屬性。 – Tomtom 2013-02-28 07:49:03
@Tomtom他只從文件中讀取並輸出內容。 – dutzu 2013-02-28 07:50:10