我正在Visual Studio 2017中创建一个项目,我需要在我创建的表单上填写一个复选框,其中包含我之前创建的文本文件的名称,并将它们发送到FTP服务器。 那可能吗? 这是我的一些代码,随时可以询问更多代码:
创建文件并保存:
using (StreamWriter writer = new StreamWriter("C:\\Users\\HP8200\\Desktop\\Faturas Teste\\Fatura " + _transaction.TransDocNumber + ".txt"))
{
string numcont = _transaction.PartyFederalTaxID;
double numenc = _transaction.BillToPartyID;
double numfatura = _transaction.TransDocNumber;
DateTime data = _transaction.CreateDate;
double valor = _transaction.TotalAmount;
int zona = transaction.UnloadPlaceAddress.AddressID;
string zona2 = transaction.UnloadPlaceAddress.AddressLine2;
double precoantesdisc = bsoItemTrans.BSOItemTransactionDetail.TransactionDetail.PackUnitPrice;
double preconet = transaction.TotalTransactionAmount;
double quantidade = transaction.Details.Count;
string bonus = "0";
string valorStr = valor.ToString(CultureInfo.InvariantCulture);
string preconetStr = preconet.ToString(CultureInfo.InvariantCulture);
string precoantesdiscStr = precoantesdisc.ToString(CultureInfo.InvariantCulture);
writer.WriteLine($"{numcont};{numenc};{numfatura};{data:dd/MM/yyyy};{valorStr};{zona};");
foreach (ItemTransactionDetail detail in transaction.Details)
{
var item = MyApp.DSOCache.ItemProvider.GetItem(detail.ItemID, MyApp.SystemSettings.BaseCurrency);
double taxRate = MyApp.DSOCache.TaxesProvider.GetTaxRateFromTaxableGroupID(detail.TaxableGroupID, "PRT", "CON");
string barcode = item.BarCode;
writer.WriteLine($"{barcode};{taxRate};{precoantesdiscStr};{preconetStr};{quantidade};{bonus}");
}
}
SqlConnection conn = new SqlConnection(@"Data source = 2c4138928627\Sage ; Database=ARMINDOData ; User Id=sa ; Password=sage2008+");
SqlCommand command = new SqlCommand("IF OBJECT_ID('UXZona', 'U') IS NULL CREATE TABLE UXZona(IDZona int,Morada char(50));", conn);
conn.Open();
SqlCommand insertCommand = new SqlCommand("INSERT INTO UXZona(FirstColumn, SecondColumn) VALUES (@0, @1)", conn);
command.ExecuteNonQuery();
SqlCommand con = new SqlCommand("IF OBJECT_ID('UXZona', 'U') IS NULL CREATE TABLE UXZona(IDZona int,Morada char(50));", conn);
MessageBox.Show("gravou");
表格代码:
在表单上我只有一个空的清单和一个提交按钮。
更新
添加了2个更具体的截图
这里我使用上面的代码创建了文本文件
我需要将该文件的名称放在此清单中,然后当我按下提交按钮时,它会将文本文件发送到我拥有的FTP服务器。
更新2: 添加了M Idrees给我的一些代码。
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
void LoadFileNames()
{
string path = @"C:\Users\HP8200\Desktop\Faturas Teste";
string fileNameOnly = "";
foreach (var item in System.IO.Directory.GetFiles(path, "*.txt")) // here you can change file extension according to your needs.
{
fileNameOnly = System.IO.Path.GetFileNameWithoutExtension(item);
checkedListBox1.Items.Add(fileNameOnly); // actually adding files names to checkbox listing
}
}
}
答案 0 :(得分:2)
我假设您在表单上添加了CheckedListBox
控件(名称checkedListBox1
)。并且需要通过从某个目录中获取文件来添加项目。如果这样就可以了。您可以使用以下代码:
private void LoadFileNames()
{
string path = @"C:\Path\To\Directory";
string fileNameOnly = "";
foreach (var item in System.IO.Directory.GetFiles(path,"*.txt")) // here you can change file extension according to your needs.
{
fileNameOnly = System.IO.Path.GetFileNameWithoutExtension(item);
checkedListBox1.Items.Add(fileNameOnly); // actually adding files names to checkbox listing
}
}
您可以从Form_Load
事件或您需要的其他事件中调用此函数。
private void Form1_Load(object sender, EventArgs e)
{
LoadFileNames();
}