c# - How to declare string with value from other form -
what trying value of uid
mainform
, declare level
in form1.
mainform
using system; using system.collections.generic; using system.drawing; using system.windows.forms; namespace sof_op_center { public partial class mainform : form { public mainform() { initializecomponent(); error.visible = false; } void loginclick(object sender, eventargs e) { if (agent.text == "user" && aid.text == "pass") { this.hide(); form1 frm = new form1(); frm.show(); } else if (agent.text == "user2" && aid.text == "pass2") { this.hide(); form1 frm = new form1(); frm.show(); } else { error.visible = true; } } } }
form1
using system; using system.io; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.text; using system.windows.forms; using system.net; namespace sof_op_center { public partial class form1 : form { public form1() { initializecomponent(); } public string server = "server"; public string user = "user"; public string pwd = "pass"; private void upload(string filename) { fileinfo fileinf = new fileinfo(filename); string uri = "ftp://" + server + "/public_html/data/" + fileinf.name; ftpwebrequest reqftp; // create ftpwebrequest object uri provided reqftp = (ftpwebrequest)ftpwebrequest.create(new uri("ftp://" + server + "/public_html/intel/" + fileinf.name)); // provide webpermission credintials reqftp.credentials = new networkcredential(user, pwd); // default keepalive true, control connection not closed // after command executed. reqftp.keepalive = false; // specify command executed. reqftp.method = webrequestmethods.ftp.uploadfile; // specify data transfer type. reqftp.usebinary = true; // notify server size of uploaded file reqftp.contentlength = fileinf.length; // buffer size set 2kb int bufflength = 2048; byte[] buff = new byte[bufflength]; int contentlen; // opens file stream (system.io.filestream) read file uploaded filestream fs = fileinf.openread(); try { // stream file upload written stream strm = reqftp.getrequeststream(); // read file stream 2kb @ time contentlen = fs.read(buff, 0, bufflength); // till stream content ends while (contentlen != 0) { // write content file stream ftp upload stream strm.write(buff, 0, contentlen); contentlen = fs.read(buff, 0, bufflength); } // close file stream , request stream strm.close(); fs.close(); } catch(exception ex) { messagebox.show(ex.message, "error uploading data."); } } private void download(string filepath, string filename) { ftpwebrequest reqftp; try { //filepath = <<the full path file created.>>, //filename = <<name of file created(need not name of file on ftp server).>> filestream outputstream = new filestream(filepath + "\\" + filename, filemode.create); reqftp = (ftpwebrequest)ftpwebrequest.create(new uri("ftp://" + server + "/public_html/intel/" + filename)); reqftp.method = webrequestmethods.ftp.downloadfile; reqftp.usebinary = true; reqftp.credentials = new networkcredential(user, pwd); ftpwebresponse response = (ftpwebresponse)reqftp.getresponse(); stream ftpstream = response.getresponsestream(); long cl = response.contentlength; int buffersize = 2048; int readcount; byte[] buffer = new byte[buffersize]; readcount = ftpstream.read(buffer, 0, buffersize); while (readcount > 0) { outputstream.write(buffer, 0, readcount); readcount = ftpstream.read(buffer, 0, buffersize); } ftpstream.close(); outputstream.close(); response.close(); } catch (exception ex) { messagebox.show(ex.message); } } public string[] getfilelist() { string[] downloadfiles; stringbuilder result = new stringbuilder(); ftpwebrequest reqftp; try { reqftp = (ftpwebrequest)ftpwebrequest.create(new uri("ftp://" + server + "/public_html/intel/")); reqftp.usebinary = true; reqftp.credentials = new networkcredential(user, pwd); reqftp.method = webrequestmethods.ftp.listdirectory; webresponse response = reqftp.getresponse(); streamreader reader = new streamreader(response.getresponsestream()); //messagebox.show(reader.readtoend()); string line = reader.readline(); while (line != null) { result.append(line); result.append("\n"); line = reader.readline(); } result.remove(result.tostring().lastindexof('\n'), 1); reader.close(); response.close(); //messagebox.show(response.statusdescription); return result.tostring().split('\n'); } catch (exception ex) { system.windows.forms.messagebox.show(ex.message); downloadfiles = null; return downloadfiles; } } void uploadclick(object sender, eventargs e) { openfiledialog opfildlg = new openfiledialog(); if (opfildlg.showdialog() == dialogresult.ok) { upload(opfildlg.filename); } } void downloadclick(object sender, eventargs e) { folderbrowserdialog flddlg = new folderbrowserdialog(); if (txtupload.text.trim().length > 0) { if (flddlg.showdialog() == dialogresult.ok) { download(flddlg.selectedpath, txtupload.text.trim()); } } else { messagebox.show("please enter file name download"); } } void showintelclick(object sender, eventargs e) { string[] filenames = getfilelist(); lstfiles.items.clear(); foreach (string filename in filenames) { lstfiles.items.add(filename); } } } } //}
you can define constructor form1
take string
, pass uid
value when in mainform
:
form1:
public partial class form1 : form { string level; public form1(string uid) { level = uid; initializecomponent(); } }
mainform:
public partial class mainform : form { string uid = "uid"; public mainform() { initializecomponent(); } void loginclick(object sender, eventargs e) { var frm = new form1(uid); this.hide(); frm.show(); } }
Comments
Post a Comment