난과 같이, 두 가지 방법에 .json 파일에서 읽어 노력하고 있어요 :두 가지 방법으로 같은 스트림을 StreamReader하는 방법?
void mainMethod()
{
Stream theFile = Assembly.GetExecutingAssembly().GetManifestResourcesStream("pathToFile");
Method1(theFile);
Method2(theFile);
}
void Method1(Stream file)
{
using (StreamReader fileUsage_1 = new StreamReader(file))
{
//do stuff with file
}
}
void Method2(Stream file)
{
using (StreamReader fileUsage_2 = new StreamReader(file))
{
//do stuff with file
}
}
가 mainMethod()
실행하면 예외를 제공합니다 : Method2()
에 Stream was not readable.
나는 문제가 Method2()
이 theFile
시를 읽는 것을 믿습니다 그것은 Method1()
에서 아직도 사용되고있다. using
블록이 자동으로 파일을 닫은 것 같습니다. theFile
을 Method1()
및 Method2()
에 "중복"하지 않도록 사용자 정의하는 방법을 사용자 정의 할 수 있습니까?
(버퍼 데이터를 폐기에 대한 자세한 내용은 Here로 이동 가능). –
@HansPassant 그래서 어떻게 해결할 수 있습니까? – user3772119
그냥 GetManifestResourcesStream()을 다시 호출하면됩니다. 또는 * leaveOpen * 인수와 함께 StreamReader 생성자를 사용합니다. –