My portal
  • Home
    • List all categories
    • Sitemap
  • Downloads
    • WebSphere
    • Hitachi902
    • Hospital
    • Kryptonite
    • OCR
    • APK
  • About me
    • Gallery
      • Italy2022
    • Curriculum vitae
      • Resume
      • Lebenslauf
    • Social networks
      • Facebook
      • Twitter
      • LinkedIn
      • Xing
      • GitHub
      • Google Maps
      • Sports tracker
    • Adventures planning
  1. You are here:  
  2. Home

GetCurrentDirectory

Details
Written by: Stanko Milosev
Category: C#
Published: 30 April 2022
Last Updated: 30 April 2022
Hits: 947
Here and here I gave examples on how to get the exe path, but sometimes that is not enough, sometimes I need to find path of our class library, for example, that is why sometimes I need Directory.GetCurrentDirectory()

Copy files but keep path structure

Details
Written by: Stanko Milosev
Category: C#
Published: 09 April 2022
Last Updated: 09 April 2022
Hits: 966
Here is one my tool to copy files which are listed in a file, and copy them to a location but keep original path tree. Usefull for TortoiseSVN usually I do it when I need to create the patch but I also want to be sure that if patch doesn't work that I have at least some kind of backup.

For example, you want to save files from c:\folder\subfolderOne\file1.txt and c:\folder\subfolderTwo\file1.txt to c:\copyFolder and to keep structure like c:\copyFolder\subfolderOne\file1.txt and c:\copyFolder\subfolderTwo\file1.txt

Here is the code:

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.

Creating valid HTTP URI

Details
Written by: Stanko Milosev
Category: C#
Published: 20 February 2022
Last Updated: 20 February 2022
Hits: 967
One example how to create URI with HTTP protocol. If we have like "www.milosev.com" and link is "2015-01-23-20-08-55/gallery", then safest way is to do it like:
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);
}

Sitemap example

Details
Written by: Stanko Milosev
Category: C#
Published: 20 February 2022
Last Updated: 29 May 2022
Hits: 880
  • xml
Here is one my example on how to create basic sitemap using serialization XML should look like:
<?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.
  1. Await and async
  2. Master - Detail example
  3. Invoke private method with reflection
  4. Example of IEnumerable casting

Subcategories

C#

Azure

ASP.NET

JavaScript

Software Development Philosophy

MS SQL

IBM WebSphere MQ

MySQL

Joomla

Delphi

PHP

Windows

Life

Lazarus

Downloads

Android

CSS

Chrome

HTML

Linux

Eclipse

Page 3 of 151

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10