gpt4 book ai didi

c# - FileNotFoundException 随机抛出

转载 作者:太空狗 更新时间:2023-10-29 17:59:28 25 4
gpt4 key购买 nike

我正在开发一个 C# 工具来从未格式化的 SD 卡中读取 8 gb 的十六进制数据。

它能够这样做,但它会随机抛出 File Not Found Exception。例如,它会读取一两千兆字节,然后将其抛出。其他时候它会连续几次读取所有 8 GB,然后抛出异常。换句话说,它似乎完全随机弹出。

我不知道是什么原因造成的。

编辑:我使用反馈来调整一些东西。下面粘贴的是更新后的代码。

它仍然随机抛出 filenotfoundexception,但现在它总是在尝试读取 gig 8 的 mb 432 时抛出参数异常(如果它到达那么远而没有随机抛出 filenotfound)。

错误提示文件句柄不支持同步操作。

class Program
{
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
static extern SafeFileHandle CreateFile(string lpFileName, uint dwDesiredAccess,
uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition,
uint dwFlagsAndAttributes, IntPtr hTemplateFile);

static void Main(string[] args)
{
string testOutputDirectory = @"C:\\Users\\aiovanna\\Desktop\\out1.txt"; //Specifies where to write the results of the read.
try
{
SafeFileHandle fileHandle = CreateFile("\\\\.\\E:", 0x80000000, 0, IntPtr.Zero, 3, 0, IntPtr.Zero);
FileStream readStream = new FileStream(fileHandle, FileAccess.Read); //The stream to be read. Is converted to binary.
BufferedStream bufStream = new BufferedStream(readStream, 1048576);
FileStream writeStream = File.OpenWrite(testOutputDirectory); //Writing stream opened at the specified directory of output.
//BinaryReader reader = new BinaryReader(readStream); //Changes the read stream to binary. Has more powerful methods.

long gigsRead; //Loop counter that specifies the number of gigabytes read thus far.
long megsRead; //Loop counter that specifies the number of megabytes read thus far within the current gigabyte.

Stopwatch totalStopwatch = new Stopwatch(); //Stopwatch to time the total execution of the card read.
Stopwatch megStopwatch = new Stopwatch(); //Stopwatch to time the execution of reading the current megabyte.
Stopwatch gigStopwatch = new Stopwatch(); //Stopwatch to time the executation of reading the current gigabyte.
totalStopwatch.Start(); //Start timing the program.
int bytesRead;

for (gigsRead = 0; gigsRead < 8; gigsRead++) //Gigabyte loop
{
gigStopwatch.Start(); //Start timer for current gigabyte.
for (megsRead = 0; megsRead < 1024; megsRead++) //Megabyte loop
{
megStopwatch.Start(); //Start timer for current megabyte.

try
{
byte[] buffer = new byte[1048576]; //Buffer to be read into from card
long test = gigsRead * 1073741824 + megsRead * 1048576;
bufStream.Position = test;
bytesRead = bufStream.Read(buffer, 0, 1048576); //Read from SD card to buffer
if (bytesRead < 1048576)
{
Console.WriteLine("Didn't read whole chunk!");
}
writeStream.Write(buffer, 0, 1048576); //Write from buffer to output text file.
megStopwatch.Stop(); //Stop timer for current megabyte.
Console.WriteLine("Finished mb {0} of gig {1} in {2}", megsRead + 1, gigsRead + 1, megStopwatch.Elapsed);
megStopwatch.Reset(); //Reset for next megabyte.
}

catch (System.IO.FileNotFoundException ex)
{
System.Console.WriteLine("The error was: {0}", Marshal.GetLastWin32Error());
System.Console.WriteLine("Message: {0}", ex.Message);
System.Console.WriteLine("Source: {0}", ex.Source);
System.Console.WriteLine("Stack Trace: {0}", ex.StackTrace);
System.Console.WriteLine("Target Site: {0}", ex.TargetSite);
System.Console.WriteLine(ex.ToString());
writeStream.Close(); //Close writing stream.
//reader.Close(); //Close the binary reader stream.
bufStream.Close();
fileHandle.Close(); //Close the SD card file.
readStream.Close(); //Close the filestream reader.
System.Console.WriteLine("You will need to turn off your computer, take out the card, turn the computer back on, put the SD card back in, and re-run the program.");
System.Console.WriteLine("Press any key to terminate.");
System.Console.ReadKey();
System.Environment.Exit(1);
}

catch (System.ArgumentException ex)
{
System.Console.WriteLine("The error was: {0}", Marshal.GetLastWin32Error());
System.Console.WriteLine("Message: {0}", ex.Message);
System.Console.WriteLine("Param Name: {0}", ex.ParamName);
System.Console.WriteLine("Source: {0}", ex.Source);
System.Console.WriteLine("Stack Trace: {0}", ex.StackTrace);
System.Console.WriteLine("Target Site: {0}", ex.TargetSite);
System.Console.WriteLine(ex.ToString());
writeStream.Close(); //Close writing stream.
//reader.Close(); //Close the binary reader stream.
fileHandle.Close(); //Close the SD card file.
readStream.Close(); //Close the filestream reader.
System.Console.WriteLine("You will need to turn off your computer, take out the card, turn the computer back on, put the SD card back in, and re-run the program.");
System.Console.WriteLine("Press any key to terminate.");
System.Console.ReadKey();
System.Environment.Exit(1);
}
}
gigStopwatch.Stop(); //Stop timer for current gigabyte.
Console.WriteLine("Finished gig {0} in {1}", gigsRead + 1, gigStopwatch.Elapsed);
gigStopwatch.Reset(); //Reset for next gigabyte.
}
totalStopwatch.Stop(); //Stop total execution timer.
Console.WriteLine(totalStopwatch.Elapsed); //Print total execution timer.
writeStream.Close(); //Close writing stream.
//reader.Close(); //Close the binary reader stream.
writeStream.Close(); //Close writing stream.
fileHandle.Close(); //Close the SD card file.
readStream.Close(); //Close the filestream reader.
bufStream.Close();
}

catch (System.IO.IsolatedStorage.IsolatedStorageException ex)
{
System.Console.WriteLine("The error was: {0}", Marshal.GetLastWin32Error());
System.Console.WriteLine("Isolated Storage Exception");
System.Console.WriteLine("Data: {0}", ex.Data);
System.Console.WriteLine("Help Link: {0}", ex.HelpLink);
System.Console.WriteLine("Inner Exception: {0}", ex.InnerException);
System.Console.WriteLine("Message: {0}", ex.Message);
System.Console.WriteLine("Source: {0}", ex.Source);
System.Console.WriteLine("Stack Trace {0}", ex.StackTrace);
System.Console.WriteLine("Target Site: {0}", ex.TargetSite);
Console.ReadKey();
}

catch (System.ArgumentException ex)
{
System.Console.WriteLine("The error was: {0}", Marshal.GetLastWin32Error());
System.Console.WriteLine("Argument Exception");
System.Console.WriteLine("Data: {0}", ex.Data);
System.Console.WriteLine("Help Link: {0}", ex.HelpLink);
System.Console.WriteLine("Inner Exception: {0}", ex.InnerException);
System.Console.WriteLine("Message: {0}", ex.Message);
System.Console.WriteLine("Param Name: {0}", ex.ParamName);
System.Console.WriteLine("Source: {0}", ex.Source);
System.Console.WriteLine("Stack Trace {0}", ex.StackTrace);
System.Console.WriteLine("Target Site: {0}", ex.TargetSite);
Console.ReadKey();
}

catch (System.IO.DirectoryNotFoundException ex)
{
System.Console.WriteLine("The error was: {0}", Marshal.GetLastWin32Error());
System.Console.WriteLine("Directory Not Found Exception");
System.Console.WriteLine("Data: {0}", ex.Data);
System.Console.WriteLine("Help Link: {0}", ex.HelpLink);
System.Console.WriteLine("Inner Exception: {0}", ex.InnerException);
System.Console.WriteLine("Message: {0}", ex.Message);
System.Console.WriteLine("Source: {0}", ex.Source);
System.Console.WriteLine("Stack Trace {0}", ex.StackTrace);
System.Console.WriteLine("Target Site: {0}", ex.TargetSite);
System.Console.ReadKey();
}

catch (System.ObjectDisposedException ex)
{
System.Console.WriteLine("The error was: {0}", Marshal.GetLastWin32Error());
System.Console.WriteLine("Object Disposed Exception");
System.Console.WriteLine("Data: {0}", ex.Data);
System.Console.WriteLine("Help Link: {0}", ex.HelpLink);
System.Console.WriteLine("Inner Exception: {0}", ex.InnerException);
System.Console.WriteLine("Message: {0}", ex.Message);
System.Console.WriteLine("Object Name {0}", ex.ObjectName);
System.Console.WriteLine("Source: {0}", ex.Source);
System.Console.WriteLine("Stack Trace {0}", ex.StackTrace);
System.Console.WriteLine("Target Site: {0}", ex.TargetSite);
Console.ReadKey();
}
}
}

下面我重写了为 filenotfoundexception 显示的错误:

Message: Unable to find the specified file.Source: mscorlibStack Trace: at System.IO.__Error.WinIOError(int32 errorcode, String maybeFullPath)at System.IO.FileStream.ReadCore(Byte[] buffer, int32 offset, int32 count)at System.IO.FileStream.Read(Byte[] array, Int32 offset, Int32 count)at System.IO.BinaryReader.Read(Byte[] buffer, Int32 index, Int32 count)at RawSDAccessTest.Program.Main(String{} args) in C:\Users\etc... at line 67Target Site: Void WinIOError(Int32, System.String)System.IO.FileNotFoundException: Unable to find the specified file.Line 67 is:reader.Read(buffer, 0, 1048576);

我在这里发现真正奇怪的是程序在第 65 行完全没问题,它也使用了 reader 对象。在执行第 65 行和第 67 行之间,它以某种方式决定该文件不再存在。我在中间等待,看看是否能解决问题。它没有。

关于可能导致它随机抛出此异常的原因或如何解决它的任何想法?

编辑:进程监视器显示以下内容

8:40:26.1077157 AM SDCardReadAttempt3.vshost.exe 2432 ReadFile E: SUCCESS 偏移量:3,228,565,504,长度:1,048,576,I/O 标志:非缓存,优先级:正常

8:40:26.1745974 AM SDCardReadAttempt3.vshost.exe 2432 ReadFile E: NO SUCH DEVICE 偏移量:3,229,614,080,长度:131,072,I/O 标志:非缓存,优先级:正常

所以在两次读取之间,设备不复存在。我将文件创建和删除移到了内部循环,这样它会在每次尝试读取文件时创建文件。问题仍然存在。我闻起来像硬件。

编辑 2:现在它偶尔会抛出异步读取异常。

9:16:16.1129926 AM SDCardReadAttempt3.vshost.exe 3752 ReadFile E: INVALID PARAMETER Offset: 7,969,177,600, Length: 1,048,576, I/O Flags: Non-cached, Priority: Normal

我不知道 .net 的深层运作方式。当文件未打开以供多个线程读取时,也许它正在将其变成一个线程进程。我会在那里等待,看看是否消除了这个错误,这样我就可以回到原来的错误。

最佳答案

我在读取扫描仪时遇到过类似的问题。我最终不得不捕获异常,等待一段时间(250 毫秒对我的目的很有效),然后再次尝试重新读取相同的数据。我定义了一个阈值(6 对我来说效果很好),在该点我放弃并向用户提出错误。

在大多数情况下,这似乎给了硬件足够的时间来 catch 进度。

此外,尝试只阅读给您带来麻烦的 block 。如果您始终在读取特定 block 时出错,那么您显然遇到了硬件问题。

关于c# - FileNotFoundException 随机抛出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16924062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com