PDA

View Full Version : سوال: مشکل در اضافه کردن کنترل ها در سی شارپ



fatima2007
دوشنبه 10 آذر 1393, 10:56 صبح
سلام
من یک برنامه کنسول اپلیکیشن دارم که میخوام تبدیل کنم به ویندوز اپلیکیشن و خروجی داده ها رو در لیست باکس بهم نشون یده ولی یک مشکلی دارم . اینکه در private static void DispatcherHandler که می خوام به جای console.write بگم در لیست باکس نشون بده اما اصلا هیچ کنترلی رو نمیشناسه.
این برنامه کنسول اپلیکیشن که درست کار میکنه:


namespace ConsoleApplication3_read_from_file
{
class Program
{
static void Main(string[] args)
{
if (args.Length == 0)
{
args = new string[] { @"C:\Users\Only God Remains\Desktop\naja prject\HTTP.pcap" };
}


// Check command line
if (args.Length != 1)
{
Console.WriteLine("usage: " + Environment.GetCommandLineArgs()[0] + " <filename>");
return;
}


// Create the offline device
OfflinePacketDevice selectedDevice = new OfflinePacketDevice(args[0]);


// Open the capture file
using (PacketCommunicator communicator =
selectedDevice.Open(65536, // portion of the packet to capture
// 65536 guarantees that the whole packet will be captured on all the link layers
PacketDeviceOpenAttributes.Promiscuous, // promiscuous mode
1000)) // read timeout
{
// Read and dispatch packets until EOF is reached
communicator.ReceivePackets(0, DispatcherHandler);
}
Console.ReadKey();
}


private static void DispatcherHandler(Packet packet)
{
// print packet timestamp and packet length
Console.WriteLine(packet.Timestamp.ToString("yyyy-MM-dd hh:mm:ss.fff") + " length:" + packet.Length);
Console.Write("Source: " + packet.IpV4.Source.ToString());
Console.WriteLine("\tDestination: " + packet.IpV4.Destination.ToString());


//object obj = packet.IpV4.Tcp.Http.Body;
//ILayer lay = packet.IpV4.Tcp.Http.ExtractLayer();








// Print the packet
//const int LineLength = 64;
//for (int i = 0; i != packet.Length; ++i)
//{
// Console.Write((packet[i]).ToString("X2"));
// if ((i + 1) % LineLength == 0)
// Console.WriteLine();
//}


Console.WriteLine();
Console.WriteLine();
}
}
}




برنامه ویندوز اپلیکیشن که خروجی نشون نمیده:


namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}


private void btnBrowseFile_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "*|*";
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
txtFilePath.Text = ofd.FileName;
}
}


private void button1_Click(object sender, EventArgs e)
{
OfflinePacketDevice selectedDevice = new OfflinePacketDevice(txtFilePath.Text);
// OfflinePacketDevice selectedDevice = new OfflinePacketDevice(@"C:\Users\Only God Remains\Desktop\naja prject\HTTP.pcap");


// Open the capture file
using (PacketCommunicator communicator =
selectedDevice.Open(65536, // portion of the packet to capture
// 65536 guarantees that the whole packet will be captured on all the link layers
PacketDeviceOpenAttributes.Promiscuous, // promiscuous mode
1000)) // read timeout
{
// Read and dispatch packets until EOF is reached
communicator.ReceivePackets(0, DispatcherHandler);


}
}


//public ListBox lb = new ListBox();
private static void DispatcherHandler(Packet packet)
{
// print packet timestamp and packet length


//Console.WriteLine(packet.Timestamp.ToString("yyyy-MM-dd hh:mm:ss.fff") + " length:" + packet.Length);
//Console.Write("Source: " + packet.IpV4.Source.ToString());
//Console.WriteLine("\tDestination: " + packet.IpV4.Destination.ToString());






ListBox lb = new ListBox();


lb.Items.Add("Source: " + packet.IpV4.Source.ToString());
lb.Items.Add("\tDestination: " + packet.IpV4.Destination.ToString());








//object obj = packet.IpV4.Tcp.Http.Body;
//ILayer lay = packet.IpV4.Tcp.Http.ExtractLayer();


// Print the packet
//const int LineLength = 64;
//for (int i = 0; i != packet.Length; ++i)
//{
// Console.Write((packet[i]).ToString("X2"));
// if ((i + 1) % LineLength == 0)
// Console.WriteLine();
//}



میشه لطف کنید بگید مشکل چیه؟

hamid_hr
دوشنبه 10 آذر 1393, 11:00 صبح
برا دسترسی به شی های فرم نباید تابع از نوع static باشه