Hi, yzhums
I appreciate your answer, however, apparently I did not explain what to do with the text file. So I put it in context a little better.
Once it has been identified which file(s) have been added to the folder, the application must access that file(s) to read them and then add the content to the BC table.
Currently I have this code, which already detects the new files in the folder and accesses them, I have also communicated to the BC object
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Net.Http;
using System.Web;
using Newtonsoft.Json.Linq;
using System.Xml.Linq;
namespace MonitorChanges
{
public partial class Form1 : Form
{
string Path = @"D:\OneDrive - ...", Ruta;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
fileSystemWatcher1.Path = Path;
GetFiles();
}
public void GetFiles()
{
string[] lst = Directory.GetFiles(Path);
textBox1.Text = "";
foreach(var sFile in lst)
{
textBox1.Text = sFile Environment.NewLine;
Ruta = sFile;
}
}
private void fileSystemWatcher1_Changed(object sender, FileSystemEventArgs e)
{
}
private async void fileSystemWatcher1_Created(object sender, FileSystemEventArgs e)
{
GetFiles();
await GetToken();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
static async Task GetToken()
{
string ClientId = "2825017b...";
string ClientSecret = "cGO8Q~...";
string TenantId = "36393c6b...";
string URL = "https://login.microsoftonline.com/" TenantId "/oauth2/v2.0/token";
HttpClient client = new HttpClient();
var content = new StringContent("grant_type = client_credentials"
"&scope=https://api.businesscentral.dynamics.com/.default"
"&client_id=" HttpUtility.UrlEncode(ClientId)
"&client_secret=" HttpUtility.UrlEncode(ClientSecret));
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-www-form-urlencoded");
var response = await client.PostAsync(URL, content);
if (response.IsSuccessStatusCode)
{
JObject Result = JObject.Parse(await response.Content.ReadAsStringAsync());
string BearerToken = Result["access_token"].ToString();
string URL2 = "https://api.businesscentral.dynamics.com/v2.0/36393c6b.../Production/ODataV4/Company('CRONUS MX')/VistaClientes";
HttpClient testclient = new HttpClient();
testclient.DefaultRequestHeaders.Add("Authorization", "Bearer " BearerToken);
var result = await testclient.GetAsync(URL2);
if (result.IsSuccessStatusCode)
{
MessageBox.Show(await result.Content.ReadAsStringAsync());
List records = new List();
List rows = File.ReadAllLines(@"D:\OneDrive...\Test.txt").ToList();
foreach (var row in rows)
{
string[] entries = row.Split(',');
Record newRecord = new Record();
newRecord.Name = entries[0];
newRecord.Description = entries[1];
newRecord.Document = entries[2];
records.Add(newRecord);
}
foreach (var record in records)
{
Console.WriteLine($"{record.Name} {record.Description}: {record.Document}");
}
}
else
{
MessageBox.Show(result.StatusCode.ToString());
}
}
else
{
MessageBox.Show(response.StatusCode.ToString());
}
}
}
}
Regards