如何在类文件中获取字符串值 [英] how to get the string value in class file
本文介绍了如何在类文件中获取字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在类文件中获取字符串值
how to get the string value in class file
string str=comboBox.SelectedItem.ToString();
这是我的课程文件
this is my class file
class FTClientCode
{
public static string curMsg = "Idle";
public static void SendFile(string fileName)
{
try
{
IPAddress[] ipAddress = Dns.GetHostAddresses(str);
IPEndPoint ipEnd = new IPEndPoint(ipAddress[0], 5656);
Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
string filePath = "";
fileName = fileName.Replace("\\", "/");
while (fileName.IndexOf("/") > -1)
{
filePath += fileName.Substring(0, fileName.IndexOf("/") + 1);
fileName = fileName.Substring(fileName.IndexOf("/") + 1);
}
byte[] fileNameByte = Encoding.ASCII.GetBytes(fileName);
if (fileNameByte.Length > 850 * 1024)
{
curMsg = "File size is more than 850kb, please try with small file.";
return;
}
curMsg = "Buffering ...";
byte[] fileData = File.ReadAllBytes(filePath + fileName);
byte[] clientData = new byte[4 + fileNameByte.Length + fileData.Length];
byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length);
fileNameLen.CopyTo(clientData, 0);
fileNameByte.CopyTo(clientData, 4);
fileData.CopyTo(clientData, 4 + fileNameByte.Length);
curMsg = "Connection to server ...";
clientSock.Connect(ipEnd);
curMsg = "File sending...";
clientSock.Send(clientData);
curMsg = "Disconnecting...";
clientSock.Close();
curMsg = "File transferred.";
}
catch (Exception ex)
{
if (ex.Message == "No connection could be made because the target machine actively refused it")
curMsg = "File Sending fail. Because server not running.";
else
curMsg = "File Sending fail." + ex.Message;
}
}
}
推荐答案
在您的课程中添加属性
In your class add a property
public static string HostAddressString {get;set;}
为组合上的IndexChanged事件编写一个事件处理程序-并在该处理程序中
Write an event handler for the IndexChanged event on the combo - and in that handler
FTClientCode.HostAddressString = combobox.SelectedItem.ToString();
因此,在该类中,您可以访问HostAddressString
so, in the class you can access HostAddressString
IPAddress[] ipAddress = Dns.GetHostAddresses(HostAddressString);
看一下CodeProject中的此链接...
使用C#的INI文件处理类 [ ^ ]
这可能会给您一个想法
Have a look at this link... from CodeProject
An INI file handling class using C#[^]
This may give you an idea
这篇关于如何在类文件中获取字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文