|
I know that.
I'm adding lines to existing files. If the existing file is ASCII and I'm adding regular ASCII text, then everything's fine. However, if the line I'm adding has some Unicode characters (which may be the case), I want to change the file's encoding to Unicode and rewrite the thing out. (If the existing file is already Unicode, it is easy of course (except for this StreamReader bug that tells you it is UTF8)).
I've figured out how to do this aready, by going through all the characters and checking for any out of the 0-255 range, but I was wondering if there was an API call, or more idiomatic way of handling this.
Matt Gerrans
|
|
|
|
|
And why do you call it a bug? It sounds correct. UTF8 is an MBCS (multi-byte character set) that uses 7-bit characters as ANSI does, but 8-bit characters (i.e., the 8th bit is set) denotes Unicode codepoints. That's the beauty of UTF8 - it maintains backward compatibility so long as you don't use Unicode, and if you must it allows for that.
So, use the UTF8Encoding instead.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
It is incorrect if the file's BOM ("\xff\xfe") says it is Unicode (or UTF16) and the reader thinks it is UTF8 ("\xef\xbb\xbf"). So I call it a bug, because I think it is one. I've since noticed that if I use the StreamReader 's string constructor, it correctly identifies it as Unicode, but if I use the FileStream constructor it mis-identifies it as UTF8. So if I do this with a Unicode encoded file:
void SomeMethod( FileInfo info )
{
StreamReader reader = new StreamReader( info.OpenRead() );
System.Text.Encoding encoding = reader.CurrentEncoding;
string data = reader.ReadToEnd();
reader.Close();
data = Massage(data);
StreamWriter writer = new StreamWriter( info.OpenWrite(), encoding );
writer.write(data);
writer.Close();
}
I get a UTF8 encoded file as a result, which I don't want. On the other hand, if I do this:
void SomeMethod( FileInfo info )
{
StreamReader reader = new StreamReader( info.FullPath );
System.Text.Encoding encoding = reader.CurrentEncoding;
string data = reader.ReadToEnd();
reader.Close();
data = Massage(data);
StreamWriter writer = new StreamWriter( info.OpenWrite(), encoding );
writer.write(data);
writer.Close();
}
The file will be Unicode, as expected (and desired). Maybe the intermediate use of the FileStream causes the loss of the encoding?
Because these files are used by multiple platforms and programming languages (not all of which support MBCS), I want to simply use either ASCII or Unicode, but not UTF8 (or UTF7 or Unicode Big-Endian, etc.). I think there is not that much beauty in UTF8 (the "backward compatibility" also get hosed by use of extended ASCII characters, which usually comes from "backward" text files that were using drawing characters and the like), just unnecessary complexity, especially in these days of multi-gigabyte storage.
By the way, the original question was about detecting the presense of Unicode characters in a string (which, having 16-bit characters could contain some, or not); this would affect the case where the original file was ASCII, but a line with some Unicode characters were inserted into it. In that case, I just want to switch the whole file over to Unicode.
Matt Gerrans
|
|
|
|
|
You should use StreamReader reader = info.OpenText(); . If you look at the IL, the code for opening a StreamReader from a stream vs. a filename is the same. The constructor which takes a string actually does the same thing you are - opens a FileStream and passes it to Init (an internal method which every constructor eventually calls).
As for your original question, see the StringInfo class defined in the System.Globalization namespace. This allows you to enumerate characters (derived from however many code points), which you could then determine if a string contains one character or more.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hi,
I noticed in a sample app source code that the app made use of a class for example a user class and then had the user objects that got created stuffed into a user collection. I was wondering why do they do this if the user object that was created existed for the life of the object why u would need a user collection? I would think u could just call the user object and get your info from that just as easily then going the extra step to put the user object into a user collection. What am I missing here?
What is the user collection giving me over just using the user objects that I can create by themselves? I hope I am explaining this correctly.
Thanks,
JJ
|
|
|
|
|
MrJJKoolJ wrote:
user class
MrJJKoolJ wrote:
user objects
MrJJKoolJ wrote:
user collection
What is a "user" class, object or collection? Do you mean one that isn't part of the .NET Framework? For the rest of my reply I'll assume that is what you mean.
MrJJKoolJ wrote:
I would think u could just call the user object and get your info from that just as easily then going the extra step to put the user object into a user collection.
There are many reasons for putting objects into a collection despite the fact they will exist elsewhere. Mostly this is to do with ease of access in the other location, the ablility to iterate over the objects and perform the same operation on all of them, the ability to create a varying number of objects and still reference them and so on.
Do you want to know more?
Vogon Building and Loan advise that your planet is at risk if you do not keep up repayments on any mortgage secured upon it. Please remember that the force of gravity can go up as well as down.
|
|
|
|
|
Hello
How can i change the title bar color?
i have using MSDN sample code but
no use link below
http://msdn.microsoft.com/library/default.asp?url=/libraray/en-us/sysinfo/base/changing_the_color_of_window_elements.asp
|
|
|
|
|
You either change it for the whole system using the Display control panel, or you complete draw the window frame yourself, which requires knowledge about the Window APIs and P/Invoke. If you want to venture down that route, I highly suggest you read all of Interoperating with Unmanaged Code[^] (pay close attention to the marshaling topics) and search this site for examples of owner-drawn window frames.
BTW, the link doesn't work.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
I have a usercontrol with a few controls on it. In the load of my usercontrol I read the settings from the registry. I also want to write these settings back to the registry when my usercontrol is unloaded. But there is no such thing as unload.
Can anyone tell me which event to use to write these settings to the registry?
|
|
|
|
|
|
That wouldn't work. Disposed happens after the control is disposed, so his controls that contains the values and any fields or properties that might reflect those values are already gone.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Handling the Disposed event is too late. You need to override Dispose(bool) , persist your data, then call base.Dispose(bool) like so:
protected override void Dispose(bool disposing)
{
PersistToRegistry();
base.Dispose(disposing);
} That way the child controls and other data is disposed after you get the data contained within. Once those controls are disposed you can't get their data (because it's been disposed).
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hi,
my chat client is easily connecting to the Server program using Sockets (in c-sharp). But
now i want to provide the functionality in my chat client program to be able to connect
through proxy. How can I do this.
Besides, I see in MSN, that there are 3 types of Proxy setting in it i.e.
- SOCKS VERSION 4
- SOCKS VERSION 5
- HTTP Proxy
would it make a major difference in the client side coding if i have to support all the
above types?
A prompt reply will be appreciated.
Regards,
Kamran
|
|
|
|
|
Your protocol has to be defined to work with proxies - it isn't automatic. The only advice anyone can really give you is to read the RFCs from http://www.ietf.com/rfc[^] for those proxy types to determine how you could include them along with your protocol definition.
HTTP, for example, allows for proxies. This is why the HTTP-related classes (like HttpWebRequest ) support proxies (a la the WebProxy ) in the .NET BCL. It's common enough to warrant inclusion in the .NET BCL. How other protocols work with proxies is up to the protocol authors. Many typically work the same (there's a lot of HTTP-like protocols, like SMTP, DICT, etc.) so I'd imagine the proxy support works the same, making it easy to use the same proxies.
What I'd recommend is using a good OO design to encapsulate your Socket client in a class and actually make use of the WebProxy for consistency (so people can reuse the same proxy; WebProxy also defaults to using the Internet Explorer settings for proxies, making it easy for your users to default to settings they might've already configured). You still need to work out how it'll work with your chat protocol, though.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
From what I can tell, the MDIList property that you used to set on a menu to make it display an MDI window list has been changed to become a MdiWindowListItem property on the MenuStrip itself
I set this to my Window menu, and it works fine when I open the first child window - Screenshot 1[^] but when I have more than one child it goes out of control: Screenshot 2[^]
(basically every time a new child window is created, it seems to add new menu items from scratch, so the existing items get left behind)
Am I doing something drastically wrong here, or is this just incomplete .NET 2.0 stuff? (I've searched everywhere for documentation on this, but haven't been able to find anything!)
--
Help me! I'm turning into a grapefruit!
Phoenix Paint - back from DPaint's ashes!
|
|
|
|
|
There's a reason they call it ".NET 2.0 Beta 1". Beta 2 is to be released early next year and you could probably expect it to be fixed by then. There's been a lot of bugs that the feature teams have been getting through so build a solid framework.
Honestly I've never used the MdiWindowListItem in a winbar, though, so I don't know if this is a common problem or if your environment enables such a bug. Therefore, I suggest you go to http://lab.msdn.microsoft.com/vs2005/[^] and search to see if this has been reported. Keep the query simple, like just "MdiWindowListItem".
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
|
benjymous wrote:
Any idea why those msdn pages aren't picked up by google?
http://lab.msdn.microsoft.com/robots.txt[^]
Again, it's beta so they probably don't want anything indexed. Some web crawlers cache for a long time.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hello,
I get this error when the program compiles, when the message box asks me to continue i can click yes and the program will still run. However, l would like to get rid of this error.
The only things l have done before l got this error was changing the startup object in the project properties and changing the Void main into another form. Thats all l think.
Could not open incremental build file 'C:\IBS LibrarySystem\LibrarySystem\obj\Debug\LibrarySystem.exe.incr' for writing
Hope someone can help me,
Steve
|
|
|
|
|
While all those changes should've touched the files so that they get rebuilt and re-linked correctly, that apparently hasn't happened.
Use Rebuild instead of Build and see if that sorts out the problem. That will clean (delete) the project targets and rebuild again.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hi,
I am using Process.Start("file.pdf") to launch the default pdf reader and open a file. This works the majority of the time. However during deployment I came across a few computers where this does not work. It does absolutely nothing. The reader is not launched and the file is not opened. However, on those computers if they manually launch their reader and then run the above code the file gets opened.
Has anyone come across this problem before? Does anyone know what is happening?
It is not a problem with the reader not being associated with the pdf file format. I know this because when they double click a pdf file from explorer it opens just fine.
Thanks,
Karl
Karl Baum
|
|
|
|
|
And what exception handling do you have around Process.Start ? Are you just catching exceptions and not doing anything with the exception data (like displaying a user-friendly error, which is the least you should do)?
Process.Start - the way you're calling it - uses ShellExecuteEx , the same API that the Windows Shell uses when you double-click files. There's really no difference.
So then you should ask yourself what the other factors may be. Are you expecting that the current working directory will be the directory in which the EXE is located? This doesn't have to be the case. The working directory can be any directory.
So, if this was some sort of help file and you expected it to be in the same directory as your application, then you should construct the fully-qualified path using either the Assembly.Location property or Application.StartupPath like so:
string path = Path.GetDirectoryName(Application.StartupPath);
path = Path.Combine(path, "help.pdf");
Process.Start(path);
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Thanks for your response. I am dealing with exceptions and none are getting thrown. It is also not a problem with the path. I can be sure of this because if you already have your reader open then the code will open the pdf file in your reader.
Why would Process.Start behave differently when the reader is already open? This may not be a problem with my code, it may be a problem with the .NET framework, the windows API, or even Adobe's stuff. I am unable to find the cause.
Thanks
Karl
|
|
|
|
|
Some applications - like Office and Adobe Acrobat/Reader - register documents (file monikers) into the ROT (running object table) associated with the application. When a new document is opened, the file persistence handler (typically; this is actually implementation-specific, but is a fundamental technology in COM) checks for running objects and if it is found, activates it. Since the application is running, it doesn't need to be opened again. The new document is opened and registered in the ROT. If this was an existing document, it activates it.
It still shouldn't behave differently, however.
Perhaps try being more explicit, like so:
ProcessStartInfo psi = new ProcessStartInfo("file.pdf");
psi.UseShellExecute = true;
psi.Verb = "open";
Process.Start(psi); Depending on the operating system this could yield different results. On Win96 the verb "open" is always assumed where with NT its whatever the default is (which could be different than "open"). You can find this out by looking at the File Types tab in the Folder Options control panel.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Thanks for your help.
I will attempt this to see if it has any impact.
Karl
Karl
|
|
|
|