Click here to Skip to main content
15,888,733 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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
Posted
Updated 3-Sep-13 21:09pm
v2
Comments
Menon Santosh 4-Sep-13 3:10am    
what is .lbl file ?
Varun_nayak 4-Sep-13 3:27am    
It is Printing Designing file, in that file we store Our Barcode Sticker image and value which are then print and stick on BOX. Some time my client want to modify tat file and want to change in value like expirary date, prise, batch no etc
Richard MacCutchan 4-Sep-13 3:20am    
The error message is telling you exactly what is wrong. Check which file you are trying to open. Note also that your Testnew.lbl file will not be in the same format as your original.
V.Lorz 4-Sep-13 3:37am    
Just a comment, it's strange to see you need to convert from binary to 'binary string'.

The code snipped seems to be correct at first sight, I've used File.ReadAllBytes() and File.ReadAllText() many times and it works.

Where exactly is the exception rising? The term 'label' in the exception message is a little strange for me. Are you opening the same file in more places?
Varun_nayak 4-Sep-13 4:25am    
No, After Creating My TESTnew.lbl, I close all application, and after m trying to open this new file, still showing error

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:
C#
using (FileStream fs = new FileStream("D:\\Work\\PNS\\TEST.lbl"))
{
    using(StreamReader reader = new StreamReader(fs))
    {
        //now read the contents
    }
}
 
Share this answer
 
Comments
V.Lorz 4-Sep-13 4:41am    
This is a good solution when processing large files as it doesn't load the entire file content into memory. But maybe for small files (up to some kilobytes) File.ReadAll---() functions are more convenient as you will require less code and the resulting code will also be more readable.
I Got the Solution by My self
Here Is the Code:

C#
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);
                   }
               }
           }
 
Share this answer
 
Comments
V.Lorz 4-Sep-13 5:51am    
Good to hear you solved it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900