权限有哪些是应该打开文件,然后输出到控制台添加行号的小程序。问题是无论程序是否从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