如何阅读.lbl文件 [英] How to read .lbl file

查看:122
本文介绍了如何阅读.lbl文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取我的.lbl文件并将其数据存储到数据库列中。因此,每当用户想要修改它时,他们都可以从数据库中创建一个新的* .lbl文件。



我已使用下面显示的代码段将我的.lbl文件数据转换为二进制文件:

  byte  [] fileBytes = File.ReadAllBytes(  D:\\ work \\PNS \\TEST.lbl ); 
StringBuilder sb = new StringBuilder();

foreach byte b in fileBytes)
{
sb.Append(Convert.ToString(b, 2 )。PadLeft( 8 ' 0'));
string bindata = sb.ToString(); // 将此变量值存储在DataBase列中
}

File.WriteAllText( D:\\Work\\PNS\\TESTnew.lbl,sb.ToString());



但是,当我打开一个新文件时,我得到像这样的错误

< br /> 
< br />
无法打开标签,文件或文件夹无法访问,不存在或已被其他用户打开。尝试打开带有只读标志的标签。





请帮我弄清楚这个问题

解决方案

而不是File类的ReadAllBytes和WriteAllText,使用例如StreamReader和StreamWriter与FileStream结合使用。并使用使用块 - 这是最重要的一点。

类似于:

 使用(FileStream fs =  new  FileStream(  D:\\Work\\PNS \\TEST.lbl))
{
使用(StreamReader reader = new StreamReader(fs))
{
// 现在阅读内容
}
}


我自己解决了这个问题

这是代码:



 使用(BinaryReader b =  new  BinaryReader(File.Open( @  D:\ work\PNS \TEST.lbl,FileMode.Open)) )
{
使用(BinaryWriter w = new BinaryWriter(File.Open( @ D:\ work\PNS\TESTnew.lbl,FileMode.Create)) )
{ // 2.
// 位置和长度变量。
int pos = 0 ;
// 2A。
// 使用BaseStream。
int length =( INT )b.BaseStream.Length;
while (pos < length)
{
// 3。
// 读取整数。
int v = b.ReadInt32();
w.Write(v);
Console.WriteLine(v);

// 4.
// 推进我们的头寸变量。
pos + = sizeof int );
}
}
}


I want to read my .lbl file and store its data into a database column, So whenever the user wants to modify it, they may create a new *.lbl file from the database.

I have converted my .lbl file data into Binary by using the snippet shown below:

byte[] fileBytes = File.ReadAllBytes("D:\\Work\\PNS\\TEST.lbl");
StringBuilder sb = new StringBuilder();

foreach (byte b in fileBytes)
{
    sb.Append(Convert.ToString(b, 2).PadLeft(8, '0'));
    string bindata = sb.ToString();  // store this variable value in DataBase Column
}

File.WriteAllText("D:\\Work\\PNS\\TESTnew.lbl", sb.ToString());


But, When I Open a new file I get Error like this

<br />
<br />
Unable to open label, the file or folder is not accessible, does not exist, or is already opened by another user. Try Opening the label with 'read-only' flag set.



Kindly help me to figure out this problem

解决方案

Instead of ReadAllBytes and WriteAllText of the File class, use methods of e.g. StreamReader and StreamWriter in combination with a FileStream. And use using blocks - that's the most important point here.
Something like:

using (FileStream fs = new FileStream("D:\\Work\\PNS\\TEST.lbl"))
{
    using(StreamReader reader = new StreamReader(fs))
    {
        //now read the contents
    }
}


I Got the Solution by My self
Here Is the Code:

using (BinaryReader b = new BinaryReader(File.Open(@"D:\Work\PNS\TEST.lbl", FileMode.Open)))
           {
               using (BinaryWriter w = new BinaryWriter(File.Open(@"D:\Work\PNS\TESTnew.lbl", FileMode.Create)))
               {// 2.
                   // Position and length variables.
                   int pos = 0;
                   // 2A.
                   // Use BaseStream.
                   int  length = (int)b.BaseStream.Length;
                   while (pos < length)
                   {
                       // 3.
                       // Read integer.
                      int v = b.ReadInt32();
                       w.Write(v);
                       Console.WriteLine(v);

                       // 4.
                       // Advance our position variable.
                       pos += sizeof(int);
                   }
               }
           }


这篇关于如何阅读.lbl文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆