파일을 열고 콘솔에 출력하여 줄 번호를 추가하는 작은 프로그램이 있습니다. 문제는 프로그램이 IDE의 명령 콘솔에서 실행되는지 여부에 관계없이 파일 권한과 관련하여 예외가 발생한다는 것입니다.파일에 대한 Visual Studio 2010 사용 권한
실행 파일과 읽을 파일 (간단한 TXT 파일)을 여러 디렉터리 (내 문서, 임시 파일 등)로 이동하여 관리 콘솔로 실행하고 Visual Studio를 관리자로 실행하고 두 파일에 모든 사용 권한을 부여했습니다. ,하지만 항상 예외를 throw합니다. 가장 이상한 일은 1 ~ 2 주 전에 시행 착오를 통해 해결책을 모색했지만 그 사실을 기억할 수 있습니다.
다음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
@Tomtom 그는 파일을 읽고 내용을 출력하고 있습니다. – dutzu