diff --git a/src/libraries/System.Net.Sockets/tests/FunctionalTests/SendPacketsAsync.cs b/src/libraries/System.Net.Sockets/tests/FunctionalTests/SendPacketsAsync.cs index 65764b452f9908..7c69f20f1446f5 100644 --- a/src/libraries/System.Net.Sockets/tests/FunctionalTests/SendPacketsAsync.cs +++ b/src/libraries/System.Net.Sockets/tests/FunctionalTests/SendPacketsAsync.cs @@ -13,21 +13,19 @@ namespace System.Net.Sockets.Tests { - public class SendPacketsAsync + public class SendPacketsAsync : IDisposable { private readonly ITestOutputHelper _log; private IPAddress _serverAddress = IPAddress.IPv6Loopback; - // Accessible directories for UWP app: - // C:\Users\\AppData\Local\Packages\\ - private string TestFileName = Environment.GetEnvironmentVariable("LocalAppData") + @"\NCLTest.Socket.SendPacketsAsync.testpayload"; - private static int s_testFileSize = 1024; - #region Additional test attributes + private TempFile _tempFile; + private static int s_testFileSize = 1024; + private string TestFileName => _tempFile.Path; public SendPacketsAsync(ITestOutputHelper output) { - _log = TestLogging.GetInstance(); + _log = output; byte[] buffer = new byte[s_testFileSize]; @@ -36,22 +34,15 @@ public SendPacketsAsync(ITestOutputHelper output) buffer[i] = (byte)(i % 255); } - try - { - _log.WriteLine("Creating file {0} with size: {1}", TestFileName, s_testFileSize); - using (FileStream fs = new FileStream(TestFileName, FileMode.CreateNew)) - { - fs.Write(buffer, 0, buffer.Length); - } - } - catch (IOException) - { - // Test payload file already exists. - _log.WriteLine("Payload file exists: {0}", TestFileName); - } + _tempFile = TempFile.Create(buffer); + _log.WriteLine($"Created file {_tempFile.Path} with size: {s_testFileSize}"); + } + + public void Dispose() + { + _tempFile.Dispose(); } - #endregion Additional test attributes #region Basic Arguments