How to create directory to the target in C during installation after publish

YUSOF 20 Reputation points
2024-02-01T06:07:51.54+00:00

Example code to publish bellow;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _WMI_
{
    public partial class Form1 : Form
    {
        private readonly string TargetPath = @"C:\VLc\";

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var watcher = new FileSystemWatcher(@"c:\")
            {
                NotifyFilter = NotifyFilters.Attributes
         | NotifyFilters.CreationTime
         | NotifyFilters.DirectoryName
         | NotifyFilters.FileName
         | NotifyFilters.LastAccess
         | NotifyFilters.LastWrite
         | NotifyFilters.Security
         | NotifyFilters.Size
            };

            watcher.Changed += OnChanged;
            watcher.Deleted += OnDeleted;
            watcher.Created += OnCreated;
            watcher.Renamed += OnRenamed;
            watcher.Error += OnError;
            watcher.Filter = "*.*";
            watcher.IncludeSubdirectories = true;
            watcher.EnableRaisingEvents = true;
            this.Opacity = 0;
            this.ShowInTaskbar = false;

        }
        private static void OnChanged(object sender, FileSystemEventArgs e)
        {
            if (e.ChangeType != WatcherChangeTypes.Changed)
            {
                return;
            }
            Console.WriteLine($"Changed: {e.FullPath}");
        }
        private static void OnDeleted(object sender, FileSystemEventArgs e) => Console.WriteLine($"Deleted: {e.FullPath}");

        private void OnCreated(object source, FileSystemEventArgs e)
        {
            try
            {
                FileInfo fileInfo = new FileInfo(e.FullPath);
                var TargetFile = Path.Combine(TargetPath, fileInfo.Name);

                File.Copy(e.FullPath, TargetFile, true);
                listBox1.Invoke((Action)(() => listBox1.Items.Add(e.FullPath + TargetFile)));
            }

            catch (Exception exception)
            {
                Debug.WriteLine(exception.Message);
            }
        }
        private void OnRenamed(object sender, RenamedEventArgs e)
        {

        }
        private static void OnError(object sender, ErrorEventArgs e) => DisplayException(e.GetException());
        /// <summary>
        /// For debug purposes
        /// For a production environment write to a log file
        /// </summary>
        /// <param name="ex"></param>
        private static void DisplayException(Exception ex)
        {
            if (ex != null)
            {
                Debug.WriteLine($"Message: {ex.Message}");
                Debug.WriteLine("Stacktrace:");
                Debug.WriteLine(ex.StackTrace);
                Debug.WriteLine("");

                DisplayException(ex.InnerException);
            }
        }

    }

}

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,650 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Minxin Yu 11,026 Reputation points Microsoft Vendor
    2024-02-01T09:15:56.2833333+00:00

    Hi, @YUSOF

    Please run you Visual Studio as Admin.
    Based on your description, I created .NET Framework Winform and was able to set C:\

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.