Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 深度C中的调试FileNotFoundException#_C#_Windows Mobile_Filestream - Fatal编程技术网

C# 深度C中的调试FileNotFoundException#

C# 深度C中的调试FileNotFoundException#,c#,windows-mobile,filestream,C#,Windows Mobile,Filestream,我在创建文件流时遇到了随机FileNotFoundException问题。文件路径是正确的,文件存在,但我仍然随机获得FileNotFoundException。如何更详细地调试这个?我正在运行Windows Mobile 6项目,这是一个例外: System.IO.FileNotFoundException: Could not find file '\Program Files\xxx\xxx\xxx-11133.bin'. at System.IO.__Error.WinIOErro

我在创建文件流时遇到了随机FileNotFoundException问题。文件路径是正确的,文件存在,但我仍然随机获得FileNotFoundException。如何更详细地调试这个?我正在运行Windows Mobile 6项目,这是一个例外:

System.IO.FileNotFoundException: Could not find file '\Program Files\xxx\xxx\xxx-11133.bin'.
   at System.IO.__Error.WinIOError(Int32 errorCode, String str)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync, String msgPath)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
internal static FInstance ReadAndDecrypt(string key, string fiId)
        {
            FileInfo fileInfo = new FileInfo(FullFilePath(fiId));

            FileStream fileStream = null;
            CryptoStream cryptoStream = null;
            GZipInputStream zipStream = null;

            try
            {
                int time = Environment.TickCount;
                fileStream = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read);

                var serializer = new XmlSerializer(typeof(FInstance));
                var FInstance = (FInstance)serializer.Deserialize(fileStream);

                return Instance;
            }
            catch (Exception e)
            {
                Log.Error(e);
                return null;
            }
            finally
            {
                if (fileStream != null)
                    fileStream.Close();
            }

        }
这是引发异常的方法:

System.IO.FileNotFoundException: Could not find file '\Program Files\xxx\xxx\xxx-11133.bin'.
   at System.IO.__Error.WinIOError(Int32 errorCode, String str)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync, String msgPath)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
internal static FInstance ReadAndDecrypt(string key, string fiId)
        {
            FileInfo fileInfo = new FileInfo(FullFilePath(fiId));

            FileStream fileStream = null;
            CryptoStream cryptoStream = null;
            GZipInputStream zipStream = null;

            try
            {
                int time = Environment.TickCount;
                fileStream = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read);

                var serializer = new XmlSerializer(typeof(FInstance));
                var FInstance = (FInstance)serializer.Deserialize(fileStream);

                return Instance;
            }
            catch (Exception e)
            {
                Log.Error(e);
                return null;
            }
            finally
            {
                if (fileStream != null)
                    fileStream.Close();
            }

        }

我想不出任何可能导致这种情况的原因,有没有办法对其进行更深入的调试?

调试应该很简单。在
try/catch
块之前将以下内容添加到代码中:

if((!fileInfo.Exists) && (Debugger.IsAttached))
{
    Debugger.Break();
}

这将使您到达文件不存在的状态。无论如何,在您的生产代码中打开文件之前,您应该先检查文件是否存在,以防止在应用程序运行时文件被删除或正在使用。

是否对同一文件多次调用该文件?您确定没有其他程序/代码正在修改该文件吗?@Hans,Windows CE中没有驱动器号。