My use case is that my user has an outlook account and I am doing automation so when a person login a 2FA is required,
so I need to copy the OTP and paste it into my application
I m writing code in c#, is there any way to connect to outlook?
my code is below but nothing showing in console
using Microsoft.Office.Interop.Outlook;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace new project
{
internal class Program
{
static void Main(string[] args)
{
Microsoft.Office.Interop.Outlook.Application outlookApp = new Microsoft.Office.Interop.Outlook.Application();
NameSpace outlookNS = outlookApp.GetNamespace("MAPI");
MAPIFolder inbox = outlookNS.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
Items items = inbox.Items;
foreach(object item in items)
{
if (item is MailItem mailItem)
{
if (mailItem.Subject.Contains("One-Time Password for Your Account"))
{
string body = mailItem.Body;
int startIndex = body.IndexOf("OTP:") + 4;
int length = 6;
string otp = body.Substring(startIndex, length);
Console.WriteLine("OTP: " + otp);
break;
}
}
}
}
}
}
I need to copy the OTP and paste it into my application
Related
i need an opc client for work, i used TitaniumAS as it's really simple, the read works fine but the write doesnt, i have the exception in the title
The tagID is correct as it works when i read it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TitaniumAS.Opc.Client.Common;
using TitaniumAS.Opc.Client.Da;
using TitaniumAS.Opc.Client.Da.Browsing;
using System.Threading;
namespace OPCDA
{
class Program
{
static void Main(string[] args)
{
TitaniumAS.Opc.Client.Bootstrap.Initialize();
Uri url = UrlBuilder.Build("Kepware.KEPServerEX.V6");
using (var server = new OpcDaServer(url))
{
server.Connect();
//creating tag group
OpcDaGroup group = server.AddGroup("MyGroup");
group.IsActive= true;
//Write
OpcDaItem int2 = group.Items.FirstOrDefault(i => i.ItemId == "Channel1.Device1.Woord");
OpcDaItem[] items = { int2 };
object[] values = { 15601 };
HRESULT[] results = group.Write(items, values);
}
}
}
}
The application I am working on must take every link saved in an excel and run it, and in case the page status is 404, I have to save each link in a new excel (by that I mean in a excel to be all, not each link in a separate excel).
So far I've managed to get every link to run, as well as create a new excel in case I manage to make it work, but unfortunately I'm stuck at this point where I don't know how or what to write in the code for save every time the status is 404 after checking the URLs in the existing Excel
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Excel;
using OfficeOpenXml;
namespace StartaproUrls
{
partial class Program
{
private static int statusCode;
private static HttpResponseMessage result;
private static object[,] valueArray;
static async Task Main(string[] args)
{
using var client = new HttpClient();
string path = #"C:\Users\stefanv\Downloads\startapro_urls.xlsx";
Application excelApp = new();
if (excelApp != null)
{
Workbook excelWorkbook = excelApp.Workbooks.Open(path);
Worksheet excelWorksheet = (Worksheet)excelWorkbook.Sheets[1];
Microsoft.Office.Interop.Excel.Range excelRange = excelWorksheet.UsedRange;
int rowCount = excelRange.Rows.Count;
int colCount = excelRange.Columns.Count;
valueArray = (object[,])excelRange.get_Value(
XlRangeValueDataType.xlRangeValueDefault);
for (int i = 1; i <= 20; i++)
{
for (int j = 1; j <= colCount; j++)
{
result = await client.SendAsync(new HttpRequestMessage(HttpMethod.Head, $"https://beta.startapro.hu{valueArray[i, j]}"));
statusCode = (int)result.StatusCode;
Console.WriteLine(statusCode); // or result
}
}
excelWorkbook.Close();
excelApp.Quit();
}
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
var file = new FileInfo(#"C:\Users\stefanv\Downloads\startapro_urlsError.xlsx");
var urlError = GetSetupData();
await CreateExcelForErrorUrls.SaveExcelFile(urlError, file);
}
public static List<UrlErrorModel> GetSetupData()
{
List<UrlErrorModel> output = new()
{
new() { UrlWithError = valueArray }
};
return output;
}
}
}
Bellow is the class for creating the Excel
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static StartaproUrls.Program;
namespace StartaproUrls
{
class CreateExcelForErrorUrls
{
public static async Task SaveExcelFile(List<UrlErrorModel> urlError, FileInfo file)
{
DeleteIFExists(file);
using var package = new ExcelPackage(file);
var ws = package.Workbook.Worksheets.Add("ErrorRaport");
var range = ws.Cells["A1"].LoadFromCollection(urlError, true);
range.AutoFitColumns();
await package.SaveAsync();
}
private static void DeleteIFExists(FileInfo file)
{
if (file.Exists)
{
file.Delete();
}
}
}
}
And Bellow is the Model for UrlError
namespace StartaproUrls
{
partial class Program
{
public class UrlErrorModel
{
public object UrlWithError { get; set; }
}
}
}
This code filters elements of certain category and finds and concatenates parameters although what needed is something a little more complex.
First of all, a person needs to be able to choose a category (out of a drop down list) or search and find the necessary ones.
And the second thing is that a user is supposed to specify what parameters he would like to combine (we have shared parameters txt fyi) and choose the order in which they are going to follow one another. Any resource on it or something similar to it would help greatly!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.UI.Selection;
namespace CombineParameters
{
[Transaction(TransactionMode.Manual)]
public class Class : IExternalCommand
{
public Result Execute(ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
//Application app = uiapp.Application;
Document doc = uidoc.Document;
//Create Filtered Element Collector and Filter
FilteredElementCollector collector = new FilteredElementCollector(doc);
ElementCategoryFilter filter = new ElementCategoryFilter(BuiltInCategory.OST_DuctFitting);
//Applying Filter
IList <Element> ducts = collector.WherePasses(filter).WhereElementIsNotElementType().ToElements();
foreach (Element e in ducts)
{
//Get Parameter values
string parameterValue1 = e.LookupParameter("AA").AsString();
string parameterValue2 = e.LookupParameter("BB").AsString();
string parameterValue3 = e.LookupParameter("CC").AsString();
string newValue = parameterValue1 + "-" + parameterValue2 + "-" + parameterValue3;
using (Transaction t = new Transaction(doc, "Set Parameter name"))
{
t.Start();
e.LookupParameter("DD").Set(newValue).ToString();
t.Commit();
}
}
return Result.Succeeded;
}
}
}
You want to combine user selected parameters in a specific order? Why dont you use a simple windows form gui.
Example
command.cs
#region Namespaces
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System;
using System.Collections.Generic;
using System.Diagnostics;
#endregion
namespace combineParameters
{
[Transaction(TransactionMode.Manual)]
public class Command : IExternalCommand
{
public Result Execute(
ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Application app = uiapp.Application;
Document doc = uidoc.Document;
Form1 form = new Form1(doc);
//Show Dialouge form
form.ShowDialog();
return Result.Succeeded;
}
}
}
Forms1.cs
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
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;
namespace combineParameters
{
public partial class Form1 : System.Windows.Forms.Form
{
//Class variable
Document revitDoc { get; set; }
public Form1(Document doc)
{
InitializeComponent();
this.revitDoc = doc;
//Create a list of the parameters you want your user to choose from
List<string> stringParameters = new List<string>
{
"textParameter1",
"textParameter2",
"textParameter3",
"textParameter4"
};
//Add list to comboboxes on form
foreach (string parameterName in stringParameters)
{
comboBox1.Items.Insert(0, parameterName);
comboBox2.Items.Insert(0, parameterName);
comboBox3.Items.Insert(0, parameterName);
}
}
private void button1_Click(object sender, EventArgs e)
{
FilteredElementCollector collector = new FilteredElementCollector(revitDoc);
ElementCategoryFilter filter = new ElementCategoryFilter(BuiltInCategory.OST_DuctFitting);
//Applying Filter
IList<Element> ducts = collector.WherePasses(filter).WhereElementIsNotElementType().ToElements();
using (Transaction t = new Transaction(revitDoc, "Set Parameter name"))
{
//Use a try and catch for transactions
try
{
t.Start();
foreach (Element duct in ducts)
{
//Get Parameter values
string parameterValue1 = duct.LookupParameter(comboBox1.Text).AsString();
string parameterValue2 = duct.LookupParameter(comboBox2.Text).AsString();
string parameterValue3 = duct.LookupParameter(comboBox3.Text).AsString();
string newValue = parameterValue1 + "-" + parameterValue2 + "-" + parameterValue3;
//do not need .ToString() when setting parameter
duct.LookupParameter("NewParameter").Set(newValue);
}
t.Commit();
}
//Catch with error message
catch (Exception err)
{
TaskDialog.Show("Error", err.Message);
t.RollBack();
}
}
}
}
}
Snip of this example inside Revit:
Example photo
I am trying to create a program that is a character counter using
a linked list and I am struggling. I have to program this in C#. If anyone can see a solution please tell me. I am have troubling using a linked list with nodes trying to show the results of counting characters. What am I missing? What do I need to add? By the way I am using visual works to run my code.
(First file)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApp17
{
class CharacterFrequency
{
private char ch;
private int frequency;
public char Ch
{
get { return ch; }
set { ch = value; }
}
public int Frequency
{
get { return frequency; }
set { frequency = value; }
}
}
}
(Second file)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApp17
{
class Program
{
static void Main(string[] args)
{
int ch;
const int InputFile = 0;
const int OutputFile = 1;
StreamReader reader = null;
StreamWriter writer = null;
//Declaring Linked List
CharacterFrequency obj = new CharacterFrequency();
LinkedList list = new LinkedList<CharacterFrequency>();
//To read the characters in the Input File.
reader = new StreamReader(File.OpenRead(args[InputFile]));
//To insert text through the loop.
while ((ch = reader.Read()) != -1)
{
//Creating my linked list.
CharacterFrequency cf = LinkedList<CharacterFrequency>
list(ch);
//Creating my nodes.
LinkedListNode<CharacterFrequency> node;
node = list.Find(cf);
//Conditions
if (node != null)
{
node.Value.Add(cf, 1); //Increase in increments.
}
else
{
list.AddLast(cf); //If it is not on the list yet, add it.
}
}
reader.Close();
//Create my output file.
writer = new StreamWriter(File.OpenWrite(args[OutputFile]));
//Display on prompt.
foreach (CharacterFrequency cf in list)
writer.WriteLine(cf.ToString());
writer.Close();
}
}
}
I would like get all the child test suites under a parent test suite in TFS using C# code. Let me know if anyone has done this.
I have create the simple app based on Microsoft.TeamFoundationServer.ExtendedClient
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.TestManagement.Client;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string teamProjectName = "TFSCMMI";
int parentTestSuiteId = 819; // or test plan id
var TFSCurrentProjectCollection = new TfsTeamProjectCollection(new Uri("http://tfs-srv:8080/tfs/defaultcollection"));
var testStore = TFSCurrentProjectCollection.GetService<ITestManagementService>();
var teamProject = testStore.GetTeamProject(teamProjectName);
var testSuite = teamProject.TestSuites.Find(parentTestSuiteId);
if (testSuite.TestSuiteType == TestSuiteType.StaticTestSuite)
{
var staticSuite = testSuite as IStaticTestSuite;
foreach (var childSuite in staticSuite.Entries)
{
Console.WriteLine("Test suite id " + childSuite.Id + " name '" + childSuite.Title + "'");
}
}
}
}
}