|
I have to this programmatically in C#.
I already have written some text into the file using C#. I want insert the next line as the first line of the text file.
but by default it always inserts it at the end.
pls help
I am working on that
Regards
Sukanya
|
|
|
|
|
You'll have to read the entire file, append your text on the beginning and then write the whole file again.
This space for rent!
My Blog
|
|
|
|
|
Try using a StreamWriter to write to your file. The underlying stream, which is accessible through the BaseStream property provides a Position property which should allow you to position at the front of the stream/file.
www.troschuetz.de
|
|
|
|
|
In short, you cannot INSERT anything anywhere you want in a text file in a single, simple operation. Unless, of course, you want to APPEND to the end of the file.
If you want to insert text as the first lines in the file, you'll have to do one of two things:
1) Open the file; Read the entire contents of the text file into memory; Close the file; Reopen it as a new file; Write out the text you want to insert at the beginning of the file; Write out the contents that you saved; Close the file
2) Rename the file to some temporary name; Open a new file with the name you want; Write out the text you want to insert; Open the temporary file; Read it in, line by line, and write it out to the new file, line by line; Close both files
Method 1 will work very fast is better on samller files. Method 2 will work better for very large files, or files that you can't know the size of ahead of time, or files that exceed RAM capacity, but is slower.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
I'm trying to find the most effecient way/most elegant way to do this. I have a Combo box that has a list of network types (all corresponding to System.Net.NetworkInformation.InterfaceType ). Now I have an if statement that is dependent on what is selected in this combo box. e.g. if "Ethernet" is selected it will be if(interfaces[i].Type == InterfaceType.Ethernet) . Now I know I could just do a massive if nesting but I want to know what you guys think would be best.
Matt Newman
Even the very best tools in the hands of an idiot will produce something of little or no value. - Chris Meech on Idiots
|
|
|
|
|
It depends what you want to do. If you are just setting a variable or two based on the result, I'd build a hashtable of type to return value, and set it in one line.
Either way, I'd always use a switch over massive if/else blocks.
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
Basically I am enumerating all of the network adapters on the system and then allowing the user to specify which type to view (only the type selected are shown). It is really only used once.
Matt Newman
Even the very best tools in the hands of an idiot will produce something of little or no value. - Chris Meech on Idiots
|
|
|
|
|
I reckon the trick is to make the value associated with the strings in the drop down equal the enum value, if that's all you need. Or, better yet, use Enum.Parse to make an enum value out of the string, and use Enum.(whatever the method is that returns a string array from an enum ) to populate it.
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
Keep in mind that InterfaceType has a backing value type of Int32. My experience with Windows Forms is limited, but if you could get an index value of some type from the combobox, you could cast that value to a variable of type InterfaceType.
i.e.:
InterfaceType selectedType = (InterfaceType) val;
(where val is the index number pulled in through the combobox)
Then you'd only have one conditional statement checking for equality:
if (interfaces[i].Type == selectedType)
Hope this helps!
~ Levi
|
|
|
|
|
Good idea... but this is the index list (of all InterfaceTypes)
94 AsymmetricDsl
37 Atm
20 BasicIsdn
6 Ethernet
26 Ethernet3MegaBit
69 FastEthernetFx
62 FastEthernetT
15 Fddi
48 GenericModem
117 GigaBitEthernet
144 HighPerformanceSerialBus
114 IPOverAtm
63 Isdn
24 Loopback
143 MultiRateSymmetricDsl
23 Ppp
21 PrimaryIsdn
95 RateAdaptDsl
28 Slip
96 SymmetricDsl
9 TokenRing
131 Tunnel
1 Unknown
97 VeryHighSpeedDsl
71 Wireless80211
So I cant just do 0 -> 0 1->1 etc
Matt Newman
Even the very best tools in the hands of an idiot will produce something of little or no value. - Chris Meech on Idiots
|
|
|
|
|
From what I understand about Combobox controls in .NET:
- The Add() method accepts an object as a parameter, and the object's ToString() return value is what actually gets displayed in the Combobox.
- The SelectedItem property returns the object (not the ToString() result) that is selected.
Perhaps it would be possible to create a new struct containing an InterfaceType field, a string field, and a simple ToString() method to make this work. When you use the SelectedItem property of the Combobox to return the currently selected object, you could simply read back its InterfaceType field.
Like I said, I've not used Windows Forms since VB6, so hopefully somebody will correct me if I am in error.
Good luck!
~ Levi
|
|
|
|
|
|
hello,
i have a dynamic control that i add the control collection.
this control is being loaded from ascx.
if i want to change, while postback, the ascx i load, i retrieve a message that i can't since
so what i want to do is to be able to delete control viewstate while postback.
can i do so?
many thanks,
Eran.
|
|
|
|
|
Your question belongs in the ASP.NET forum.
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 an imagelist, created in the IDE, and it generated this code for me :
this.imageList.ImageSize = new System.Drawing.Size(23, 23);
this.imageList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList.ImageStream")));
this.imageList.TransparentColor = System.Drawing.Color.Lime;
the second line generates this error:
+ System.SystemException {"Could not find any resources appropriate for the specified culture (or the neutral culture) in the given assembly. Make sure \"DetailsActions.resources\" was correctly embedded or linked into assembly \"RDC\".\r\nbaseName: DetailsActions locationInfo: RDC.DetailsActions resource file name: DetailsActions.resources assembly: RDC, Version=1.0.1035.19142, Culture=neutral, PublicKeyToken=null"} System.SystemException
Now, I don't see any way to make the images or the image list build into the assembly, but I don't think that's the problem. I've found some stuff on google, but nothing that's been suggested has helped.
The resourceSets property on the resource object is 0. That is created like this :
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(DetailsActions));
The details actions class is in a subfolder called controls, I've tried adding controls to the path. This project was actually in VB.NET, I just converted it using a tool, but the VB.NET code looks the same to me, and it works.
My default namespace for the project is RDC, the root namespace of the project. Any suggestions anyone can offer are truly appreciated.
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
The DetailsActions.resources - generated from DetailsActions.resx - was not embedded into the assembly. You won't see this by default in the IDE, so you'll need to go to Project->Show Hidden Files to see it. It should be nested under the form or control to which it's related. The images or image strip that you assigm to an ImageList is base64-encoded into a ResX file, which gets generated into a .resources file and embedded.
If you want to forego the ResX file, then add the images or image list to your project and set the Build Action to Embedded Resource. To create the image or images to add to the ImageList , use something like the following:
Image img;
using (Stream s = GetType().Assembly.GetManifestResourceStream("NameSpace.ImageStrip.png"))
img = Image.FromStream(s); Make sure that "NameSpace" is the fully-qualified namespace of the object you added. So, if your project's default namespace is "RDC" and your image was called "ImageStrip.png" under a folder named "Example", then the fully-qualified manifest resource name is "RDC.Example.ImageStrip.png". Any embedded resources - even .resources files - use this convention unless you explicitly embedded the resources into the assembly (i.e., not using VS.NET or re-assemblying the assembly).
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]
|
|
|
|
|
OK, I can see the resx file now, and it's an embedded resource, so it must be in there.
I've inherited this project, the only form in which I have the bitmaps is this project file, where they are embedded in the imageList, with the obvious exception that I cannot get them out. How do I go about turning the list into a collection of bitmaps so I can load them into an image list manually ? Or, preferably, how can I make this code work as it stands ?
If I look at the XML, I can see the image stream there. I've tried all sorts of variations in the path I pass in to the resource object. Is there a way I can query it to find out the path I need ?
Thanks for your help.
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
The resources object is created like this:
System.Resources.ResourceManager resources = new System.Resources.ResourceManager("DetailsActions", System.Reflection.Assembly.GetExecutingAssembly());
I tried controls.DetailsActions ( because it's in a folder called 'controls' ) to no avail. I've tried various other permutations, and I've also established that all my other image lists ( that reside in the root folder ) are loading fine. I've also reverified that the existing code works perfectly in the VB.NET version. I have no idea what to do next...
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
The ResourceManager should - if the file "path" in the manifest is the same as the fully-qualified class name - use new ResourceManager(typeof(DetailsActions)) .
Make sure the .resources file is in there. Open the assembly in ildasm.exe (installed by the Framework SDK, which is installed with VS.NET by default, into the SDK's Bin directory), double-click "MANIFEST", and search for "RDC.controls.DetailsActions.resources" below the assembly attributes.
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 found this:
.mresource public RDC.controls.DetailsActions.resources
{
}
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
Then in your DetailsActions.cs (assuming similar class and filename) do this:
new ResourceManager(typeof(DetailsActions)) The ResourceManager uses the Type namespace and class name and appends ".resources" to get the resource. If you were to specify the resource yourself, you would have to do:
new ResourceManager("RDC.controls.DetailsActions.resources", Assembly.GetExecutingAssembly()); Though the example isn't right, you might - for sanity's sake - look in the ResX file to make sure a data name with "imageList1.Images" (or whatever the key is) exists in the file.
Any class defaults to using the project's root namespace (configured in the project) + any folders. This is for C#. VB.NET, on the other hand, builds the namespace up for each folder and is not cummulative. Make sure your class files don't repeat namespace parts for folder that already exist.
Resources, in both cases, are handled like classes in VB.NET: the "namespace" is built from the project root namespace + any folders down the line to the file to embed.
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]
|
|
|
|
|
Heath Stewart wrote:
Though the example isn't right, you might - for sanity's sake - look in the ResX file to make sure a data name with "imageList1.Images" (or whatever the key is) exists in the file.
Did that - it's there. It's .ImageStream, I believe, either way, it's the same thing the code that blows up is looking for.
Heath Stewart wrote:
new ResourceManager(typeof(DetailsActions))
Sorry, it did that originally, and I've changed it back. I should have said when you mentioned it before.
I really appreciate your help with this.
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
Make sure your source file in VB.NET doesn't include the full namespace, like:
Namespace RDC
Namespace controls
Public Class DetailsActions
' ...
End Class
End Namespace
End Namespace What will happen, if the project's root namespace is "RDC" already, and this in a folder calls "controls", is that you'll have a fully-qualified class as RDC.controls.RDC.controls.DetailsActions .
If you're using the other overload for ResourceManager to specify the name, specify exactly the name you saw in the assembly manifest when using ildasm.exe, including the extension.
I feel so dirty writing VB.NET code in the C# forum.
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]
|
|
|
|
|
In VB.NET, it doesn't include a namespace at all. Could this be the problem ?
Heath Stewart wrote:
If you're using the other overload for ResourceManager to specify the name, specify exactly the name you saw in the assembly manifest when using ildasm.exe, including the extension.
I'm using the typeof one again now, because that's what it was originally, I was just looking for a way to specify the namespace, but it didn't help.
Heath Stewart wrote:
I feel so dirty writing VB.NET code in the C# forum
*grin* what's happening here is that we're being paid to work on some code that was in VB, and we refused unless we could convert it to C#. We did this, using a conversion tool, and the authors of the tool can't tell us why this is happening, although it has something to do with the subfolder, all the other image lists are not in subfolders, and work fine.
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|