|
As it turns out, changing the code to the listing below resolved my issue.
<br />
FileStream fStream = File.OpenRead(sourceFiles[i].ToString());<br />
byte[] buffer = new byte[fStream.Length];<br />
int bytesRead;<br />
bytesRead = fStream.Read(buffer, 0, buffer.Length);<br />
<br />
<br />
fStream.Close();<br />
Decoder decoder = Encoding.Default.GetDecoder();<br />
char[] cBuffer = new char[buffer.Length];<br />
int bytesConverted, charsConverted;<br />
bool bCompleted;<br />
<br />
decoder.Convert(buffer, 0, buffer.Length, cBuffer, 0, buffer.Length, false, out bytesConverted, out charsConverted, out bCompleted);<br />
Just because we can; does not mean we should.
|
|
|
|
|
KaptinKrunch wrote: changing the code to the listing below resolved my issue.
That means that the file is not at all Unicode, but ANSI.
KaptinKrunch wrote: bytesRead = fStream.Read(buffer, 0, buffer.Length);
Ouch. You read from the file, but you ignore the result. The Read method doesn't have to read as much data as you request. You have to loop until all data is read, or the method return zero.
The easiest is of course to just replace all that code with:
string text = File.ReadAllText(sourceFiles[i].ToString(), Encoding.Default);
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Hi all,
Any one suggest me how to get a file from a folder using the partial file name. It's similar to wildcard search in database.
For eg:
in my folder there are files with following name.
sample_text.txt, sample_data.txt,samplefile,data_text.txt
in the above files i have to get the file list which name starts with sample_ like this.
is it possible to do this using regular expression?
Please suggest me
Thanks in advance
Know is Drop, Unknown is Ocean
|
|
|
|
|
Use the System.IO.Directory.GetFiles() method. Set your search expression to 'sample_*.*'.
Paul Marfleet
"No, his mind is not for rent
To any God or government"
Tom Sawyer - Rush
|
|
|
|
|
Thank you very much for your replay. I tried the above suggestion. But it returning me Illegal Charter path error.
My sample code:
string filenamesss = Directory.GetFiles("c:\\samplefiles\\20*.pdf");
Know is Drop, Unknown is Ocean
|
|
|
|
|
Try this if the documentation you read does not make it clear. BTW, Directory.GetFiles returns a string array!
string[] filenamesss = Directory.GetFiles("c:\\samplefiles\\", "*.PDF", System.IO.SearchOption.TopDirectoryOnly);
OR
<br />
string[] filenamesss = Directory.GetFiles("c:\\samplefiles\\", "*.PDF", System.IO.SearchOption.AllDirectories);
Just because we can; does not mean we should.
|
|
|
|
|
|
Hi gareth,
Thousands of thanks to you. It's really working good. also i got an chance to learn more things from that link.
Thank you once again to all
Know is Drop, Unknown is Ocean
|
|
|
|
|
Is it possible to create static extension methods in the latest .Net framework? I presently have the following scenario:
I have a bottom level project, call it Base, that defines some objects that
are common accross multiple solution files (lets use UInt128 as an example)
I have a Web Service API project, call it WebAPI, that only contains a web reference
I have a main project file, call it Main, that has a reference to both Base and WebAPI The problem is that WebAPI contains a class, WebAPI.UInt128, that is logically equivalent to Base.UInt128. Now, I want to add an explicit operator to case a WebAPI.UInt128 into a Base.UInt128 and back. Since neither the WebAPI nor the Base projects have a reference to both types, I wanted to define this operator in the place that makes the most sence, which is within the Main project. Is there some way to do this? Thanks in advance,
Sounds like somebody's got a case of the Mondays
-Jeff
|
|
|
|
|
Yes with framework 3.5 you can create extension methods. Although I would think it better to define it in base and have WebAPI use that reference. Creating a class with the same name in different namespaces, with presumably different functionality will lead to confusing code that is difficult to maintain.
only two letters away from being an asset
|
|
|
|
|
I actually don't create two different classes with the same name... Visual Studio does it for me. Basically, the WebAPI.UInt128 only contains the serialized data from within the Base.UInt128 class, even though both are logically equivalent. However, since the WebAPI is automatically generated and in actuallity does not depend on Base, I should not need to add that reference to the WebAPI project. For a client that DOES have access to both WebAPI and Base, that client should be able to use either UInt128 class interchangeably. That is why I asked for such a thing.
Mark Nischalke wrote: Yes with framework 3.5 you can create extension methods.
Could you please provide an example of how I would declare the following from within the Main project? I am having a hard time finding any documentation describing how to externally define static extension methods.
public static explicit operator WebAPI.UInt128(Base.UInt128 other) { ... } Thanks,
Sounds like somebody's got a case of the Mondays
-Jeff
|
|
|
|
|
Extension methods are created as static methods on static classes with the this keyword in front of the first parameter. For instance, suppose you want to check that a string isn't empty, and that it isn't just made up of spaces then you could use this:
public static class StringExtensions
{
public static bool IsTrimmedNullOrEmpty(this string value)
{
bool returnValue = false;
if (string.IsNullOrEmpty(value) || value.Trim() == string.Empty)
returnValue = true;
return returnValue;
}
} Then you would just call this with
string myString = " ";
if (myString.IsTrimmedNullOrEmpty())
{
Console.WriteLine("This is an empty string");
}
|
|
|
|
|
I have seen examples of such a thing plenty of times online. However, what I am attempting to do is add a type cast operator, which would be a static method in the string class. Your example is actually perfect for what I am trying to do, as myString CANNOT be null given that you added an instance call. Therefore, in the example you gave, is there a way to define a method identical to what you proposed, but that would be called in the following way (so that myString could actually be null without throwing an exception):
if (string.IsTrimmedNullOrEmpty(myString)) { ... } Thanks,
Sounds like somebody's got a case of the Mondays
-Jeff
|
|
|
|
|
You can't make extension operators. Even if you make them manually (with ExtensionAttribute and SpecialNameAttribute) the compiler doesnt pick them up.
|
|
|
|
|
That was my suspicion, but I wanted to be certain prior to proceeding. Thanks for your help,
Sounds like somebody's got a case of the Mondays
-Jeff
|
|
|
|
|
I have seen code like this what does new is doing on the declaration of this function. Is it better to define this way or it's some kind of shortcut. Thanks
public Class A:UserControl
{
public new int Width
{
}
}
|
|
|
|
|
netJP12L wrote: what does new is doing on the declaration of this function.
They hide that information in the documentation[^]
led mike
|
|
|
|
|
I have a custom string being spit out from a log file that I would like make into a DateTime object.
The problem is I have no idea how to do this simply.
Mon Feb 25 07:21:00 2008
The string does not parse straight to a DateTime object and I do not have anyway of changing the format of the incoming string.
|
|
|
|
|
|
Thanks, didn't realize that function was for doing that kind of parsing.
Took me just a few secs to have it up and running properly.
|
|
|
|
|
Sunset Towers wrote: didn't realize that function was for doing that kind of parsing.
Yeah, darn Microsoft keeps hiding the kind of information in the documentation.
led mike
|
|
|
|
|
Hi ho
I've noticed something weird. When I try to read an unexisting address location (using ReadProcessMemory of kernel32 libraries), it gives an exception. This is totally normal. Then I'm able to retrieve the error code with GetLastError(). This code can be formatted, which returns a nice string message in my native language.
But now I've noticed something that's not quite normal. When I run the application normally, and the expected exception happens, the FormatMessage returns something like:
"A Read/WriteProcessMemory-operation is only partly executed."
Riiiiiight... Now the weirdest thing is, if I put a debugging breakpoint just before I retrieve the error code with GetLastError(), and then do a step-by-step running (you know, the F11/F10 thing), it returns something totally different:
"Tried to point to an unexisting token."
Which is the message I want, because that's exactly what I did wrong. But still he doesn't return it if I don't put a breakpoint just before the GetLastError() function. It's as if the system needs a little time to store the error code, and my program is too fast requesting it. I tried a System.Threading.Thread.Sleep(100); or so, but that didn't work.
Here's my code, if that helps:
<br />
if (!ReadProcessMemory(handle, new IntPtr(address), buffer, length, out bytesReadPtr))<br />
{<br />
StringBuilder str = new StringBuilder();<br />
str.Capacity = 1000;<br />
FormatMessage(<br />
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,<br />
IntPtr.Zero,<br />
GetLastError(),<br />
0,<br />
str,<br />
1000,<br />
IntPtr.Zero);<br />
throw new Exception("Unable to read process memory.\n" + str.ToString());<br />
}<br />
Anyone knows what's going on?
Thanks in advance.
(Btw. Can't I use decent tabs in code on this forum? )
modified on Sunday, March 2, 2008 12:03 PM
|
|
|
|
|
Dear all,
I wrote this piece of code to clear all the textboxes on a control all at once.
foreach (Control control in tabpage.Controls)<br />
{<br />
if (control.GetType() == typeof(TextBox));<br />
{<br />
control.Text = "";<br />
}<br />
}
It works , but .. it also clears all the labels in the control ; however labels are not of type Textbox.
Could somebody tell me what I am doing wrong ?
kind regards,
|
|
|
|
|
Addition to the previous post ... this works :
for (int i = 0; i < this.Controls.Count; i++ )<br />
{<br />
if (this.Controls[i] is TextBox)<br />
{<br />
this.Controls[i].Text = "";<br />
}<br />
}
I'm puzzled now
|
|
|
|
|
Rick van Woudenberg,
foreach (Control control in Controls)
{
if (control is TextBox)
{
TextBox textBox = control as TextBox;
textBox.Text = string.Empty;
}
}
Regards,
Gareth.
|
|
|
|
|