- Details
- Written by: Stanko Milosev
- Category: C#
- Hits: 1354
using System; using System.IO; namespace CopyFilesKeepPathStructure { class Program { static void Main(string[] args) { Console.WriteLine("Root source path"); string rootSourcePath = Console.ReadLine(); //string rootSourcePath = @" c:\folder"; Console.WriteLine("Root destination path"); string rootDestinationPath = Console.ReadLine(); //string rootDestinationPath = @"c:\copyFolder"; Console.WriteLine($"File with list of files to be copied:"); string fileWithListOfFilesToBeCopied = Console.ReadLine(); if (!(fileWithListOfFilesToBeCopied is null) && !(rootSourcePath is null)) { string[] listOfSourceFiles = File.ReadAllLines(fileWithListOfFilesToBeCopied); //string[] listOfSourceFiles = File.ReadAllLines(@"C:\files.txt"); foreach (string sourceFile in listOfSourceFiles) { string folderStructure = sourceFile.Substring(rootSourcePath.Length, sourceFile.Length - rootSourcePath.Length); string destinationFile = rootDestinationPath + folderStructure; Directory.CreateDirectory(Path.GetDirectoryName(destinationFile) ?? string.Empty); File.Copy(sourceFile, destinationFile, true); Console.WriteLine($"Source: {sourceFile}, destination: {destinationFile}"); } } Console.WriteLine($"Press any key..."); Console.ReadKey(); } } }Download from here.
- Details
- Written by: Stanko Milosev
- Category: C#
- Hits: 1328
Uri domain = new UriBuilder("milosev.com").Uri; if (Uri.TryCreate(domain, "2015-01-23-20-08-55/gallery", out Uri myUri)) { Console.WriteLine(myUri.AbsoluteUri); }
- Details
- Written by: Stanko Milosev
- Category: C#
- Hits: 1255
<?xml version="1.0" encoding="utf-8"?> <urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xhtml="http://www.w3.org/1999/xhtml" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <loc>http://www.example.com/</loc> <lastmod>2005-01-01</lastmod> <changefreq>monthly</changefreq> <priority>0.8</priority> </url> </urlset>The model:
using System.Collections.Generic; using System.Xml.Schema; using System.Xml.Serialization; namespace SiteMapXmlCreation { public class Model { [XmlRoot("urlset")] public class Urlset { [XmlAttribute("schemaLocation", Namespace = XmlSchema.InstanceNamespace)] public string SchemaLocation { get; set; } [XmlElement(ElementName = "url")] public List<url> Url { get; set; } } public class url { [XmlElement(ElementName = "loc")] public string Loc { get; set; } [XmlElement(ElementName = "lastmod")] public string Lastmod { get; set; } [XmlElement(ElementName = "changefreq")] public string Changefreq { get; set; } [XmlElement(ElementName = "priority")] public string Priority { get; set; } } } }The Program:
using System.Collections.Generic; using System.IO; using System.Xml.Serialization; namespace SiteMapXmlCreation { class Program { static void Main(string[] args) { Model.Urlset serializedUrlset = new Model.Urlset { Url = new List<Model.url> { new() { Changefreq = "monthly" , Lastmod = "2005-01-01" , Loc = "http://www.example.com/" , Priority = "0.8" } } , SchemaLocation = "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" }; TextWriter txtWriter = new StreamWriter(Path.ChangeExtension(System.Reflection.Assembly.GetEntryAssembly().Location, ".xml")); XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add(string.Empty, "http://www.sitemaps.org/schemas/sitemap/0.9"); ns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance"); ns.Add("xhtml", "http://www.w3.org/1999/xhtml"); XmlSerializer xmlSerializer = new XmlSerializer(typeof(Model.Urlset), "http://www.sitemaps.org/schemas/sitemap/0.9"); xmlSerializer.Serialize(txtWriter, serializedUrlset, ns); txtWriter.Close(); } } }Example download from here.
- Details
- Written by: Stanko Milosev
- Category: C#
- Hits: 1210
using System; using System.Threading; using System.Threading.Tasks; namespace AwaitAsyncExample { class Program { static void Main(string[] args) { Task.Run(async () => { await MultipleAwaits(); }); Console.ReadKey(); } static async Task MultipleAwaits() { Task task1 = Task.Run(() => { Thread.Sleep(2_000); Console.WriteLine("Done with first task!"); }); Task task2 = Task.Run(() => { Thread.Sleep(1_000); Console.WriteLine("Done with second task!"); }); Task task3 = Task.Run(() => { Thread.Sleep(1_000); Console.WriteLine("Done with third task!"); }); await Task.WhenAll(task1, task2, task3); } } }Another example:
private static readonly Object ThisLock = new Object(); static void Main(string[] args) { Task.Run(async () => { await MultipleAwaits(); }); Console.ReadKey(); } static async Task MultipleAwaits() { Task task1 = Task.Run(() => { lock (ThisLock) { File.AppendAllText("test.txt", "test 1"); } }); Task task2 = Task.Run(() => { lock (ThisLock) { File.AppendAllText("test.txt", "test 2"); } }); Task task3 = Task.Run(() => { lock (ThisLock) { File.AppendAllText("test.txt", "test 3"); } }); await Task.WhenAll(task1, task2, task3); }Notice:
lock (ThisLock) { File.AppendAllText("test.txt", "test 2"); }Without lock exception "The process cannot access the file 'test.txt' because it is being used by another process." will be raised. One more example in Windows forms:
namespace AsyncTest; public partial class Form1 : Form { public Form1() { InitializeComponent(); btnStart.Click += DoSomeWork; } async void DoSomeWork(object sender, EventArgs e) { label1.Text = "start"; string text = await DoWait(); label1.Text = text; } private Task<string> DoWait() { Task<string> task1 = Task.Run(() => { Thread.Sleep(2_000); return Task.FromResult("did it"); }); return task1; } }