2014-11-14 77 views
0

我解析大文本文件,它的工作很好,但一段时间后,它给了我异常(System.Core.dll中发生未经处理的类型'System.UnauthorizedAccessException'异常阅读和解析文本文件异常-C#

其他信息:对路径的访问被拒绝)

我得到下面提线异常。

accessor = MemoryMapped.CreateViewAccessor(offset, length, MemoryMappedFileAccess.Read); 

下面是我的功能

public static void CityStateZipAndZip4(string FilePath,long offset,long length,string spName) 
    { 
     try 
     { 

      long indexBreak = offset; 
      string fileName = Path.GetFileName(FilePath); 
      if (fileName.Contains(".txt")) 
       fileName = fileName.Replace(".txt", ""); 

      System.IO.FileStream file = new System.IO.FileStream(@FilePath, FileMode.Open,FileAccess.Read, FileShare.Read); 

      Int64 b = file.Length; 
      MemoryMappedFile MemoryMapped = MemoryMappedFile.CreateFromFile(file, fileName, b, MemoryMappedFileAccess.Read, null, HandleInheritability.Inheritable, false); 

      using (MemoryMapped) 
      { 

       //long offset = 182; // 256 megabytes 
       //long length = 364; // 512 megabytes 


       MemoryMappedViewAccessor accessor = MemoryMapped.CreateViewAccessor(offset, length, MemoryMappedFileAccess.Read); 


       byte byteValue; 
       int index = 0; 
       int count = 0; 
       StringBuilder message = new StringBuilder(); 
       do 
       { 
        if (indexBreak == index) 
        { 
         count = count + 1; 
         accessor.Dispose(); 

         string NewRecord = message.ToString(); 
         offset = offset + indexBreak; 
         length = length + indexBreak; 
         if (NewRecord.IndexOf("'") != -1) 
         { NewRecord = NewRecord.Replace("'", "''"); } 

         // string Sql = "insert into " + DBTableName + " (ID, DataString) values(" + count + ",'" + NewRecord + "')"; 
         string Code = ""; 
         if (spName == AppConfig.sp_CityStateZip) 
         { 
          Code = NewRecord.Trim().Substring(0, 1); 

         } 

         InsertUpdateAndDeleteDB(spName, NewRecord.Trim(), Code); 
         accessor = MemoryMapped.CreateViewAccessor(offset, length, MemoryMappedFileAccess.Read); 
         message = new StringBuilder(); 
         index = 0; 

         //break; 
        } 

        byteValue = accessor.ReadByte(index); 
        if (byteValue != 0) 
        { 
         char asciiChar = (char)byteValue; 
         message.Append(asciiChar); 
        } 
        index++; 
       } while (byteValue != 0); 

      } 
      MemoryMapped.Dispose(); 
     } 

     catch (FileNotFoundException) 
     { 

      Console.WriteLine("Memory-mapped file does not exist. Run Process A first."); 
     } 

    } 

回答

0

此异常意味着你的计划没有得到Read访问从Windows文件。
当您的程序尝试读取文件时,是否确保该文件未被锁定?
例如,它可能是您自己的程序当前正在使用的文件。
如果不是,请尝试以管理员身份运行程序,看看它是否有所作为。

+0

我以管理员身份运行,但没有区别,我的程序工作1分钟,然后发生异常。 – Sherry 2014-11-24 07:19:52

+0

你可以调试程序,看看发生了什么异常吗? – 2014-11-24 20:43:44

+0

异常来自accessor = MemoryMapped.CreateViewAccessor(offset,length,MemoryMappedFileAccess.Read); – Sherry 2014-12-04 16:31:14

0

资源处理代码某处深,我们有这样的事情:

try { 
// Try loading some strings here. 
} catch { 
// Oops, could not load strings, try another way. 
} 

抛出异常,并且已经处理,它永远不会在你的应用程序显示出来。看到它的唯一方法是附加调试器并观察此消息。你可以从代码中看到,它与你的问题没有任何关系。这里真正的问题是调试器向你显示你不应该看到的东西。

在没有调试模式下运行解决方案,它工作正常。