|
Ignore this post... as now i get it.. i trully was ignoring your post went to fast through the text u wrote
Thanks for the answers all of you.
And thank you Giorgi, as you have, once again, solved my problem.
Have a nice day
Regards,
Matjaž
modified on Monday, November 24, 2008 5:49 AM
|
|
|
|
|
I'm not insisting but if I were you I would use BinaryReader/BinaryWriter. If you need to replace first 55 bytes do something like this:
1. Write 55 bytes with BinaryWriter
2. Read 55 bytes by BinaryReader. Don't do anything with them, just read.
3. Read other bytes from file by BinaryReader and write them with BinaryWriter.
|
|
|
|
|
Thats it... i'm almost on giving up on programming, as im going nowhere.
Could you, please, for my better understaning, write me this piece of code for this problem... i just dont get it.
You also said that write first 55 bytes. Ok, but there shouldnt be those first 55 bytes.
This program is ment for this: i got back around 20 PDF files and all of them have 55 bytes at the beggining which makes them fail on load in a viewer. Yes, i would already fix them by hand, but if the problem appears again it would be better to have an app. to do that for me.
Once again... sample would be the best for me. A working one and tested ... which would delete first 55 bytes or rewrite the file without those 55 bytes...
Bah... im desperate :/
Regards,
Matjaž
|
|
|
|
|
I thought you need to change first 55 bytes. If not just remove step one from my previous post.
|
|
|
|
|
that means nothing from a sample?
Regards,
Matjaž
|
|
|
|
|
OK, here you go:
int length;
byte[] bt=new byte[1024];
FileStream input=new FileStream("in.pdf", FileMode.Open);
FileStream output=new FileStream("out.pdf", FileMode.Create);
BinaryReader rd= new BinaryReader(input);
BinaryWriter rw=new BinaryWriter(output);
rd.Read(bt, 0, 55);
while(true)
{
length=rd.Read(bt, 0, 1024);
if (length==0) break;
rw.Write(bt);
}
rd.Close();
rw.Close();
input.Close();
output.Close();
It should work. By the way, it's better to use using block to make sure that streams are always closed.
modified on Monday, November 24, 2008 8:36 AM
|
|
|
|
|
With the indexes fixed:
byte[] x= File.ReadAllBytes("C:\\test.pdf");
byte[] temp = new byte[x.Length - 55];
long tempx = 0;
for (long i = 55; i < x.LongLength; i++)
{
temp[tempx] = x[i];
tempx++;
}
File.WriteAllBytes("C:\\test.pdf", temp);
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Great!!! This is what i needed. Works perfect!! Thanks Guffa for taking your time and looking at my code.
Thanks again to all of you, again, for taking your time and trying to help.
Regards,
Matjaž
|
|
|
|
|
While Guffa's solution works, it's about three-six times slower depending on input size than my solution.
|
|
|
|
|
Maybe, but i dont know how to use it, thats why i was asking for a sample.
I coded the program the way i knew, but got a problem and Guffa fixed my index out of bounds problem... but your solution... i dont know how to use it.
If u want, u can write your sample/example... but if you dont want to, this code is fine for me.
Regards,
Matjaž
|
|
|
|
|
|
Dont know how could i miss your reply, but ok. Tnx.
Learn by myself? I like problems a lot... and i try to get my knowledge on my own and with help from someone else. Then i combine these two things and most of the times, i get good results. For a begginer, ofcourse. This is my way of learning...
Regards,
Matjaž
|
|
|
|
|
Ok, but with using your code, i get this error:
************* Exception Text **************<br />
System.IO.IOException: The process cannot access the file 'C:\Documents and Settings\milos\My Documents\Visual Studio 2008\Projects\Brisanje Bytov iz datotek\Brisanje Bytov iz datotek\bin\Debug\New Folder\export_104974_00001484.pdf.e3.blob.pdf' because it is being used by another process.<br />
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)<br />
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)<br />
at System.IO.FileStream..ctor(String path, FileMode mode)<br />
at Brisanje_Bytov_iz_datotek.Form1.button2_Click(Object sender, EventArgs e) in C:\Documents and Settings\milos\My Documents\Visual Studio 2008\Projects\Brisanje Bytov iz datotek\Brisanje Bytov iz datotek\Form1.cs:line 37<br />
at System.Windows.Forms.Control.OnClick(EventArgs e)<br />
at System.Windows.Forms.Button.OnClick(EventArgs e)<br />
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)<br />
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)<br />
at System.Windows.Forms.Control.WndProc(Message& m)<br />
at System.Windows.Forms.ButtonBase.WndProc(Message& m)<br />
at System.Windows.Forms.Button.WndProc(Message& m)<br />
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)<br />
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)<br />
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)<br />
Why is the file in use? Im sure it isnt :P
Regards,
Matjaž
|
|
|
|
|
Perhaps some program has your file open or didn't close properly. I tested my code on multiple times and it works like a charm. Unlocker[^] can help to find out which program is using your file.
|
|
|
|
|
Thanks. Forgot about unlocker. Will try tommorow at work.
Regards,
Matjaž
|
|
|
|
|
Waiting for results
|
|
|
|
|
Goodmorning in a new day.
I've had my files locked, because i was trying to rewrite the same file. Unlocker didnt show anything. Thought i could change the output to the same filename as input.
Ok, fixed this.. but now im trying to get this thing into a loop. I have a listbox with filenames/paths. Looks like this:
for (int c = 0; c < listBox1.Items.Count; c++)
{
listBox1.SelectedItem = c;
int length;
byte[] bt = new byte[1024];
FileStream input = new FileStream(listBox1.Text, FileMode.Open);
FileStream output = new FileStream(listBox1.Text + ".new", FileMode.Create);
BinaryReader rd = new BinaryReader(input);
BinaryWriter rw = new BinaryWriter(output);
rd.Read(bt, 0, 55);
while (true)
{
length = rd.Read(bt, 0, 1024);
if (length == 0) break;
rw.Write(bt);
}
rd.Close();
rw.Close();
input.Close();
output.Close();
}
Well, it goes only through the first item (that i manualy selected). Where seems to be the problem? I'm focused at that "break;", but i guess that only breaks while loop? or it also breaks my for loop...
Thanks.
Regards,
Matjaž
|
|
|
|
|
You don't need listBox1.SelectedItem = c; Just access i-th item like this: string text =listbox1.Items[i].ToString();
|
|
|
|
|
Ok. Fixed and working. Thanks. If u want, take a look at my new question, regarding tiff images and going through pages of tiff.
Thanks and have a nice, successful day
Regards,
Matjaž
|
|
|
|
|
Glad to help you
I already did but I unfortunately I don't know how to do it. If you want vote for the answers I have given to you
|
|
|
|
|
If it helps... i'll vote for some
Regards,
Matjaž
|
|
|
|
|
It just shows appreciation from you and works as an encouragement. Thank you
|
|
|
|
|
i used the code like this to pervent overflow:
string deletethis = "";
using (var reader = new StreamReader(@"C:\<>\U2\" + props["File"].Replace("%20", " ")))
{
deletethis = reader.ReadLine();
deletethis = deletethis + reader.ReadLine();
deletethis = deletethis + reader.ReadLine();
deletethis = deletethis + reader.ReadLine();
deletethis = deletethis + "ream" + Environment.NewLine+""+Environment.NewLine;
reader.Close();
reader.Dispose();
};
byte[] bytes = Encoding.ASCII.GetBytes(deletethis);
byte[] x = File.ReadAllBytes(@"C:\<>\U2\" + props["File"].Replace("%20", " "));
byte[] temp = new byte[x.Length - bytes.Length];
Int64 tempx = 0;
for (Int64 i = bytes.Length; i < x.LongLength; i++)
{
temp[tempx] = x[i];
tempx++;
}
File.WriteAllBytes(@"C:\<>\U2\" + props["File"].Replace("%20", " "), temp);
|
|
|
|
|
1. Use BinaryReader and BinaryWriter to read and write file in binary mode.
2. Don't read the whole file at once. If the file is to large you might get an exception.
|
|
|
|
|
Hi all,
I have to Disable 'Start' button untill the thread completes.
btn_Start_Click(object sender, EventArgs e)
{
Thread th = new Thread(new ThraedStart(Download));
th.Start();
if (th.ThreadState == ThreadState.Running) // Here i am Disabling
btn_Start.Enabled = false;
// But where i have write logic for Enabling the Button
//like if(th.ThreadState==ThreadState.Stopped) btn_Start.Enabled = true;
}
public void Download()
{
// Here i wrote logic for downloading files.
}
Please suggest me how to Disable Button until thread completes.
Thanks in Advance
|
|
|
|