|
Hi,
I'm wondering if it is possible to save an instance of a class to a file (as is) and vice versa.
I have a class that can contain about 2000 ethernet packages. I would like to save this entire class to a file, but also be able to open it again for later use. Something like: MyClass = (MyClassType)FileStream ???
Any ideas??
G
|
|
|
|
|
You need to look at something called serialization.
Heres a simple example
using System;
using System.Runtime.Serialization.Formatter.Binary;
using System.IO;
[Serializable()]
public class MyClass
{
public int MyInt;
private int privateInt;
public MyClass(int pub, int pri)
{
MyInt = pub;
privateInt = pri;
}
public void PrintOut()
{
Console.WriteLine("MyInt = {0}, privateInt = {1}", MyInt.ToString(), privateInt.ToString());
}
}
public class Driver
{
public static void Main()
{
MyClass myClass = new MyClass(4, 2);
FileStream stream = new FileStream("C:\\myclass.bin", FileMode.CreateNew, FileAccess.Write);
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(myClass);
stream.Close();
myClass = null;
stream = new FileStream("C:\\myclass.bin", FileMode.Open, FileAccess.Read);
myClass = (MyClass) formatter.Deserialize(stream);
stream.Close();
myClass.PrintOut();
}
} If you want to customize what is saved in the serialization process refer to the documentation on the ISerializable interface
HTH,
James
Simplicity Rules!
|
|
|
|
|
With File.GetLogicalDrives() I can get name of drives,but how can I check if they are CD-ROM or Floppy drives?
Mazy
"The path you tread is narrow and the drop is shear and very high,
The ravens all are watching from a vantage point near by,
Apprehension creeping like a choo-train uo your spine,
Will the tightrope reach the end;will the final cuplet rhyme?"Cymbaline-Pink Floyd
|
|
|
|
|
Mazy,
Someone has already written a detailed article about this here on CP. Here is the link:
DriveComboBox
HTH
Nick Parker
|
|
|
|
|
Thank you,I'll check it as soos as I go home.
Mazy
"The path you tread is narrow and the drop is shear and very high,
The ravens all are watching from a vantage point near by,
Apprehension creeping like a choo-train uo your spine,
Will the tightrope reach the end;will the final cuplet rhyme?"Cymbaline-Pink Floyd
|
|
|
|
|
I have a file and waana check if it has ReadOnly attribute,if its true change it to write in it. I use this code:
if(File.GetAttributes(filename) == FileAttributes.ReadOnly)
File.SetAttributes( filename , FileAttributes.Normal );
FileStream myfile = File.Open( filename ,System.IO.FileMode.Open , System.IO.FileAccess.ReadWrite );
But when it reached to readonly file it does not change the ReadOnly attribute.
Mazy
"The path you tread is narrow and the drop is shear and very high,
The ravens all are watching from a vantage point near by,
Apprehension creeping like a choo-train uo your spine,
Will the tightrope reach the end;will the final cuplet rhyme?"Cymbaline-Pink Floyd
|
|
|
|
|
Try this instead
if( (File.GetAttributes(filename) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly )
James
Simplicity Rules!
|
|
|
|
|
Thank you,I'll check it as soos as I go home.
Mazy
"The path you tread is narrow and the drop is shear and very high,
The ravens all are watching from a vantage point near by,
Apprehension creeping like a choo-train uo your spine,
Will the tightrope reach the end;will the final cuplet rhyme?"Cymbaline-Pink Floyd
|
|
|
|
|
Anybody knows how to attack F1 Help with the controls in C#? Example if you press F1 in a Text Box a message pop up with the help text.
|
|
|
|
|
Use the HelpRequested event (HelpEventHandler is the delegate instead of HelpRequestedEventHandler).
James
Simplicity Rules!
|
|
|
|
|
Hello, i was wondering if there are functions to deal with the AVI in C# like CAVIEdit in C++, functions like AVIFileOpen,.......
Thanx in advance
|
|
|
|
|
You can use P/Invoke and COM interop to access the functions in the Windows Multimedia SDK. There aren't any .NET classes that wrap those methods, yet.
James
Simplicity Rules!
|
|
|
|
|
I'd say the authorative answer whenever dealing with AVI would be Virtual Dub (Google will tell you).
|
|
|
|
|
I have two 'byte' and I want to do Xor() operation with these two value,How can I do it?
Mazy
"The path you tread is narrow and the drop is shear and very high,
The ravens all are watching from a vantage point near by,
Apprehension creeping like a choo-train uo your spine,
Will the tightrope reach the end;will the final cuplet rhyme?"Cymbaline-Pink Floyd
|
|
|
|
|
I believe the ^ operator is what you want to use there.
James
Simplicity Rules!
|
|
|
|
|
James T. Johnson wrote:
I believe the ^ operator is what you want to use there.
Yes,thats it.
Mazy
"The path you tread is narrow and the drop is shear and very high,
The ravens all are watching from a vantage point near by,
Apprehension creeping like a choo-train uo your spine,
Will the tightrope reach the end;will the final cuplet rhyme?"Cymbaline-Pink Floyd
|
|
|
|
|
I want to do some operation when the application run,but after my form become visible,If I put my operation in construtor or Load ,it does the operation before the form become visible.
Mazy
"The path you tread is narrow and the drop is shear and very high,
The ravens all are watching from a vantage point near by,
Apprehension creeping like a choo-train uo your spine,
Will the tightrope reach the end;will the final cuplet rhyme?"Cymbaline-Pink Floyd
|
|
|
|
|
maybe something like
class Form1 : System.Windows.Forms.Form
{
bool shown = false;
...
private void Form1_Activated(object sender, System.EventArgs e)
{
if (!shown)
{
shown = true;
MessageBox.Show("I'm here.");
}
}
}
does the job for you.
the "Activated" event with a bool to show it only once (after construction)
not really nice, because everytime the form is activated this comparison takes place, but hey: in times of gigahertz and garbage-collectors - who cares about one function call and one comparison
:wq
|
|
|
|
|
Thank you,I'll check it as soos as I go home.
Mazy
"The path you tread is narrow and the drop is shear and very high,
The ravens all are watching from a vantage point near by,
Apprehension creeping like a choo-train uo your spine,
Will the tightrope reach the end;will the final cuplet rhyme?"Cymbaline-Pink Floyd
|
|
|
|
|
Actually there is an even better way if you are really concerned about performance.
class Form1 : System.Windows.Forms.Form
{
public Form1()
{
InitializeComponent();
this.Activated += new EventHandler(Activated_PerformOnce);
}
private void Activated_PerformOnce(object sender, System.EventArgs e)
{
this.Activated -= new EventHandler(Activated_PerformOnce);
}
} Amazing what comes to you while you sleep :-P
James
Simplicity Rules!
|
|
|
|
|
1.I hava Datagrid control in window forms, which bind data from datatable. I don't want to show "id" field, but I think I need it for click event on a certain row in datagrid. How can I hide this field but still can get value from it?
2. How can I specify the column width for each column?
Thanks.
Beginner
|
|
|
|
|
For #1 give this a shot
int columnIndex = 1;
GridColumnStylesCollection myGridColumns;
myGridColumns = dataGrid1.TableStyles[0].GridColumnStyles;
myGridColumns.RemoveAt( columnIndex ); For #2 there try this block.
GridColumnStylesCollection myGridColumns;
myGridColumns = dataGrid.TableStyles[0].GridColumnStyles;
foreach(DataGridColumnStyle style in myGridColumns)
{
style.Width = GetColumnWidth(style.HeaderName);
} HTH,
James
Simplicity Rules!
|
|
|
|
|
I want to set the image properties of items in a listview/treeview to various memstream/filestreams I have (that are images).
Any idea how I can do this?
Cheers,
Simon
"I get paid for my brain and my thinking in several obscure worlds", Olli, The Lounge
|
|
|
|
|
You'll have to create a new instance of the Image class from the contents of the stream.
treeNode.Image = Image.FromStream(myStream);
HTH,
James
Simplicity Rules!
|
|
|
|
|
What is the .NET equivalent for CButton::GetState?
Any help is hugely appreciated.
Thank you,
Nish
The rumours that I am an AI bot are absolutely false. These rumours have been propogated by *them* to focus all the attention on to me, while *their* bots take over the planet. Thank y%%%% Divide by zero. Cannot proceed. Abort(y/y)?
|
|
|
|