|
Try the following:
using (StreamWriter sw = File.AppendText(Path))
{
sw.BaseStream.Position = 1;
sw.WriteLine("Hello World");
}
www.troschuetz.de
|
|
|
|
|
I tried out the code but i got a runtime error at sw.BaseStream.Position=0;(It is zero based).
System.IO.IOException: Unable seek backward to overwrite data that previously existed in a file opened in Append mode.
what to do?
Sukanya
|
|
|
|
|
I took a deeper look into the documentation and think you'll have to use one of the other suggested solutions.
The File.AppendText(path) method is equivalent to StreamWriter(path, true) . The boolean parameter append "determines whether data is to be appended to the file. If the file exists and append is false, the file is overwritten. If the file exists and append is true, the data is appended to the file. Otherwise, a new file is created."
So in the current solution, append is true and seeking backward is forbidden. By replacing File.AppendText(path) with StreamWriter(path, false) you should be able to seek backward, but as the above text state the existing file content will be overwritten.
Sorry and more luck with the other suggestion
www.troschuetz.de
|
|
|
|
|
The word "Append" literally means "Add to the end" - so they have named this method correctly it seems.
With the lack of a "Prepend" method, you will need to use the above solution of setting position to zero and using Write or WriteLine
|
|
|
|
|
Hello gurus,
I would like to know how I can process the Key events in a dialog box?
I have controls (buttons, radio buttons etc.) in my dialog box and I have to process the Left/Right/Down/Space keys. The problem is that when I override the OnKeyDown(object sender, System.Windows.Forms.KeyEventArgs e) method of the form, the focus of the controls in my form moves while I press the arrow keys... got it?
So, how can I avoid the process of the default behaviour of these keys?
In MFC a simple PreTranslateMessage was there, but in C#... how to to that???
Thanks for the help.
Best regards.
There is no spoon.
|
|
|
|
|
|
is there a simpler way?
There is no spoon.
|
|
|
|
|
If you need the form to process all key events for any controls, then set Form.KeyPreview to true .
While the other controls have focus and without KeyPreview being set the messages are handled only by the active control's message handler (WndProc in managed code).
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]
|
|
|
|
|
ah, ok it works.
now no matter the control I insert, the form handles the KeyDown event
thanks.
There is no spoon.
|
|
|
|
|
hi all,
I have an application, where there is a service manager which is responsible for controlling three services. The three services (which r controlled by service manager) have exe files. I have installed these three services using the installutil.exe . Now, i cannot install the service manager in the same way as I installed the three services because service installer is not present for the service manager application.
What i have to do now, Should i add installer to the service manager application and install it using installutil.exe....... if, so , how are the other three services controlled by this application.......
|
|
|
|
|
I'm not sure I fully understand your question. Is the service manager project, itself, a service? If it is, then yes, add an installer project for the service project, compile it and install it.
If, however, the service manager project is just a standard executable or a web project (doubtful), then it should be possible to compile and execute the manager from the windows desktop.
As to how the other three services are controlled by the manager, you would need to provice more implementation details about the service manager project for me to answer that question.
The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’
|
|
|
|
|
Just copy it to a directory. If the three services are installed the service manager application using the ServiceController doesn't need to be in the same directory as the services. The ServiceController (which encapsulates the service control manager (SCM) native APIs) works with service names and doesn't care about the path once the service is registered (and, BTW, all services are executables).
The service manager application will need to know the names of the services only. Where you put it on the local machine (and you can even control services remotely if you have the necessary privileges) doesn't matter.
Read Deploying Applications[^] in the .NET Framework SDK for more information.
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 guess you all know about PE files' timestamps. The are in hex, and for example
41DE6BF7 means 07/01/2005 11:01:11.
I tried many ways to 'convert' the timestamp to a DateTime object, but they all failed. (DateTime.FromFileTime(...), FromTicks(...)
My question is, how to 'convert' the hex value to a DateTime.
Thanks in advance and best regards,
Stan
|
|
|
|
|
No, timestamps are not in hex. Hexadecimal is only a numeric representation. They're a number and that's all. To actually be stored in hex would require storing a string, which I know is now the case.
To convert numbers to a filetime, you can pass the number (a long , or Int64 ) to either of the static DateTime.FromFileTime or DateTime.FromFileTimeUtc methods. In your case, you'll want to use DateTime.FromFileTimeUtc since timestamps are specified in UTC (+0 GMT).
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 am wondering how to develop an application with multi-language. What's the common practise for this? I don't think people will change every single word from English to other language. Any good approach for this?
Thanks,
Leo
|
|
|
|
|
luozhan1 wrote:
I don't think people will change every single word from English to other language.
That would really piss off people. Imagine if I am using an application like IE and I have a Menu called "Archivo" (Spanish for file) and then in that menu (because you couldn't be bother to change all the words) were "Open" and "Exit" instead of "Abrir" and "Cerrar". That would be confusing (especially as Exit is very similar to the Spanish word for "success")
luozhan1 wrote:
Any good approach for this?
You should read up on the subjects of Localisation, Globalisation and Internationalisation (you may need to replace my -sation with -zation as I am localised to UK-English and the documentation is in US-English)
Do you want to know more?
WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and Forums
|
|
|
|
|
Colin Angus Mackay wrote:
That would really piss off people.
That's a good way of putting it. Here at Microsoft and other global companies geopolitical issues are a major concern. What you may not find offensive some other group of people may.
There are books on this issue. One that's required reading here is Developing International Software, Second Edition[^]. It's definitely worth a read if you're seriously planning on localizing your code.
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]
|
|
|
|
|
The SDK has a wealth of information on localisation (long live Australian English! ); in fact I just started reading up on the topic myself - I want to implement localisation into my ASP.NET applications for work.
The common approach (from what I have been reading) is to use satellite assemblies which are loaded dynamically depending on the user's current CultureInfo.CurrentUICulture setting. If a satellite assembly isn't available for their UI culture, use the default.
However, I don't know anything more than that - I haven't even got it working myself yet
This space for rent!
My Blog
|
|
|
|
|
|
Those are for ASP.NET. Assuming they did it the right way, Windows Forms applications are still localized a little differently.
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]
|
|
|
|
|
You should start by reading Developing World-Ready Applications[^] in the .NET Framework SDK.
Basically, in whatever language you develop your application, that text (and other objects which have an associated TypeConverter ) is your neutral resource language and is compiled into your primary assembly (the assembly that contains the IL modules compiled from your code. Any other languages you develop are put into satellite assemblies.
If you're using VS.NET this is very easy (but, until "Whidbey", pretty inefficient at runtime for larger applications). Develop your forms and controls, then select the top-level control (like the Form or UserControl ) and find the Localized property (this is actually a design-time property; it is not defined on the class). Set that to true then find the Language property (again, a design-time property). Set that to something else and change the text, positions and sizes of controls, etc.
If you want to see what VS.NET is doing behind the scenes, click Project->Show hidden files from the menu. You'll see a lot of .ResX files nested underneat your control class files. The neutral resource .ResX will not have an ISO lang/locale identifier (like MyClass.resx) while the others will (like MyClass.de-DE.resx or MyClass.es.resx, which are resource files for Germany (German language) and the Spanish language (no locale), respectively.
When you compile your application, satellite assemblies are created. If you then look into your source code, expand the hidden regions and you'll see lots of calls to ResourceManager.GetString or ResourceManager.GetObject (actually, an instance of ResourceManager typically named resources ).
When the CultureInfo.CurrentUICulture (not CurrentCulture , which is used for formatting dates, times, and numbers - it specifies the locale, not the language) is set the ResourceManager will try to find a language for that. If any locale is specified in that CultureInfo instance, that locale will be used if it exists. If not the fallback is the language. If a satellite assembly doesn't exist for that, the neutral resources - those inside your primary assembly - are used.
To make this faster, in your AssemblyInfo.cs file (actually, wherever is fine) define the assembly-level attribute NeutralResourcesLanguage set to the "lang-locale" for the language in which your project is developed, like "en-US" for US English. If the ResourceManager needs to find those reosurces, it will not not to go probing for them (which is especially slow if deploying your managed application using touchless deployment across the Internet or even an intranet).
Be sure to read that link I gave you, as well as some of the classes you'll be using like ResourceManager and CultureInfo . Taking a look at what VS.NET does (because that's about what you'd do if you did it manually) is also extremely helpful.
If you use VS.NET to do this your clients can even localize your application without the source using winres.exe from the .NET Framework SDK, or a separate localization team if you have such an entity. You can use this if you do it manually, but you must understand what is required that winres.exe looks for, which you can learn by looking to see what VS.NET does.
Here's the catch: VS.NET declares instantiates the ResourceManager in InitializeComponents . If you want to support switching languages in the middle of the application (which you can do by setting CultureInfo.CurrentUICulture , then you'll need to re-initialize - not re-instantiate - your controls. To do this efficiently you'll need to break VS.NET's design-time support by abstracting the initialization out of InitializeComponents , leaving only the instantiation in there (or more the instantiation to another method). This means you can call a method to re-initialize the controls (how you trigger this is up to you). This is pretty uncommon for larger projects, as many problems can occur and since most people don't need to switch languages in the middle of a Windows session.
Note, too, that MUI (multi-user interfaces API) automatically sets CultureInfo.CurrentUICulture (actually, the property is initialized from the current MUI language). So, if a user is using a German Windows installation or the German MUI as I sometimes do, CultureInfo.CurrentUICulture would automatically be set to "de-DE" so your application - if localized for "de-DE" or just "de" - would automatically display your German text and control positions, sizes, etc.
If you'd like more information about how "Whidbey" is improving efficency while using the designer to localize applications, read my blog entry at http://blogs.msdn.com/heaths/archive/2004/10/05/237987.aspx[^].
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]
|
|
|
|
|
|
You should look over the System.Drawing and System.Drawing.Drawing2D namespaces. C# has a rich API for drawing many of the things you mention (e.g. gradients, buttons, etc.) Here is a short tutorial on some basic drawing/painting functions: http://www.c-sharpcorner.com/2/gdi_plus.asp[^]
You should also look into Pens, Brushes, Patterns, and Alpha Blending (Transparency)[^].
Best Regards.
-Matt
------------------------------------------
The 3 great virtues of a programmer:
Laziness, Impatience, and Hubris.
--Larry Wall
|
|
|
|
|
I am using a security dongle (clients request) and am having some problems with it. They have released a .net component to talk to the device. I have tried speaking to them about my problem, but they are on the other side of the planet, and communication is way to slow.
Ok, this code works 100% in any C# program, except asp.net. I am not going to give you the code I am using, as it would be of no use in this problem. The dll attempts to connect to a USB port on the computer. However, in asp.net this fails.
So my question is this: The asp.net page does not actually need to connect to the device, as all it needs to do is store some information on there. What sort of component should I make that can be called from asp.net? I have not used COM or windows services, or anything else like that, so am not sure if one of these could help me. Please let me know how I can call a method in a remote object to do this work for me?
|
|
|
|
|
Is the asp.net executing on the local machine or on a remote server ?
Due to obvious security issues you can't access the local file system from a webpage and nor can you access any device.
The only workaround would be to use a smart client.
"Do not inflate plain things into marvels, but reduce marvels to plain things." - Francis Bacon
|
|
|
|
|