locked
Unable to create folder in clcikonce installed location. RRS feed

  • Question

  • I am trying to create a folder at clickonce installed location as backup folder to copy files into the folder from clickonce location in c#,the folder is created sometimes and sometimes it is not created.What can i do to create a folder programatically using c#. I am sharing my code.

     if (ApplicationDeployment.IsNetworkDeployed)
                    {                
                        ApplicationDeployment dep = System.Deployment.Application.ApplicationDeployment.CurrentDeployment;                    
                        if (dep.IsFirstRun)
                        {
                            
                            DirectoryInfo dir = Directory.CreateDirectory(Path.Combine(ClickOnceLocation, "Backup"));
                            string[] files1 = System.IO.Directory.GetFiles(ClickOnceLocation);
                            foreach (string s in files1)
                            {
                                string fileName = System.IO.Path.GetFileName(s);                      
                                string destFile = System.IO.Path.Combine(ClickOnceLocation+"\\Backup\\" , fileName);              
                                System.IO.File.Copy(s, destFile,true);                          
                                File.Delete(s);
                            }
                          
                        }
    
                    }     

    Thanks,

    Gayatri



    • Edited by Gayatri KD Wednesday, February 20, 2019 11:15 AM
    Wednesday, February 20, 2019 10:57 AM

Answers

  • Hi Gayatri KD,

    First, could you tell me how did you get the ClickOnce installed location? By assigning a value to a string?

    If so, to backup all the content in a folder, maybe you can refer to this simple demo:

        /// <summary>
        /// string sourcePath = "C:\\Users\\username\\Desktop\\New folder";
        /// string targetPath = "C:\\Users\\username\\Desktop\\New folder\\Backup folder";
        /// </summary>
        /// <param name="sourceDirectory"></param>
        /// <param name="targetDirectory"></param>
        public void directoryCopy(string sourceDirectory, string targetDirectory)
        {
            try
            {
                DirectoryInfo dir = new DirectoryInfo(sourceDirectory);
                // Get files and subdirectories under the directory (without subdirectories)
                FileSystemInfo[] fileinfo = dir.GetFileSystemInfos();
                foreach (FileSystemInfo i in fileinfo)
                {
                    if (i is DirectoryInfo)     // Determine if the folder
                    {
                        if (!Directory.Exists(targetDirectory + "\\" + i.Name))
                        {
                            // Create a subfolder without this folder in the target directory
                            Directory.CreateDirectory(targetDirectory + "\\" + i.Name);
                        }
                        // Recursively copy subfolder
                        directoryCopy(i.FullName, targetDirectory + "\\" + i.Name);
                    }
                    else
                    {
                        // Copy file if is not a folder, true means that files with the same name can be overwritten
                        File.Copy(i.FullName, targetDirectory + "\\" + i.Name, true);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("error");
            }
        }

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Gayatri KD Thursday, February 21, 2019 12:25 PM
    Thursday, February 21, 2019 2:22 AM
  • Hi Kyle,

    Yes, It is giving long path name,creating folder inside another folder.

    Even if multiple folders are created,I want only files to be copied to the backup folder,I do not to copy folders.

    I used the following code to copy only files.

      public void directoryCopy(string sourceDirectory, string targetDirectory)
            {
                try
                {
                    DirectoryInfo dir = new DirectoryInfo(sourceDirectory);
                    // Get files and subdirectories under the directory (without subdirectories)
                    FileSystemInfo[] fileinfo = dir.GetFileSystemInfos();
                    if (!Directory.Exists(targetDirectory + "\\"))
                    {
                        // Create a subfolder without this folder in the target directory
                        Directory.CreateDirectory(targetDirectory + "\\");
                    }
    
                    foreach (FileSystemInfo i in fileinfo)
                    {
                        if (i is FileInfo)
                        {
                            // Copy file if is not a folder, true means that files with the same name can be overwritten
                            File.Copy(i.FullName, targetDirectory + "\\" + i.Name);
                           
                        }
                        
                    }
                        
                }
                catch (Exception ex)
                {
                   MessageBox.Show(ex.Message);
                }
            }

    Thanks a lot,

    Gayatri



    • Edited by Gayatri KD Thursday, February 21, 2019 12:24 PM
    • Marked as answer by Gayatri KD Thursday, February 21, 2019 12:26 PM
    Thursday, February 21, 2019 10:46 AM

All replies

  • Hi Gayatri KD,

    First, could you tell me how did you get the ClickOnce installed location? By assigning a value to a string?

    If so, to backup all the content in a folder, maybe you can refer to this simple demo:

        /// <summary>
        /// string sourcePath = "C:\\Users\\username\\Desktop\\New folder";
        /// string targetPath = "C:\\Users\\username\\Desktop\\New folder\\Backup folder";
        /// </summary>
        /// <param name="sourceDirectory"></param>
        /// <param name="targetDirectory"></param>
        public void directoryCopy(string sourceDirectory, string targetDirectory)
        {
            try
            {
                DirectoryInfo dir = new DirectoryInfo(sourceDirectory);
                // Get files and subdirectories under the directory (without subdirectories)
                FileSystemInfo[] fileinfo = dir.GetFileSystemInfos();
                foreach (FileSystemInfo i in fileinfo)
                {
                    if (i is DirectoryInfo)     // Determine if the folder
                    {
                        if (!Directory.Exists(targetDirectory + "\\" + i.Name))
                        {
                            // Create a subfolder without this folder in the target directory
                            Directory.CreateDirectory(targetDirectory + "\\" + i.Name);
                        }
                        // Recursively copy subfolder
                        directoryCopy(i.FullName, targetDirectory + "\\" + i.Name);
                    }
                    else
                    {
                        // Copy file if is not a folder, true means that files with the same name can be overwritten
                        File.Copy(i.FullName, targetDirectory + "\\" + i.Name, true);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("error");
            }
        }

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Gayatri KD Thursday, February 21, 2019 12:25 PM
    Thursday, February 21, 2019 2:22 AM
  • Hi Kyle,

    I got the clickonce location using following code assigned to string,I need to create a new folder in that location as backup and copy all the files to the backup folder.

    System.Reflection.Assembly assemblyInfo = System.Reflection.Assembly.GetExecutingAssembly(); string assemblyLocation = assemblyInfo.Location; // MessageBox.Show(assemblyLocation); Uri uriCodeBase = new Uri(assemblyInfo.CodeBase); string ClickOnceLocation = Path.GetDirectoryName(uriCodeBase.LocalPath.ToString());//Path for cliconce installed location


     if (ApplicationDeployment.IsNetworkDeployed)
                    {                
                        ApplicationDeployment dep = System.Deployment.Application.ApplicationDeployment.CurrentDeployment;                    
                        if (dep.IsFirstRun)
                        {                       
                            DirectoryInfo dir = Directory.CreateDirectory(Path.Combine(ClickOnceLocation, "Backup"));//create backup folder
                            string[] files1 = System.IO.Directory.GetFiles(ClickOnceLocation);
                            foreach (string s in files1)
                            {
                                string fileName = System.IO.Path.GetFileName(s);
                                string destFile = System.IO.Path.Combine(ClickOnceLocation + "\\Backup\\", fileName);
                                System.IO.File.Copy(s, destFile, true);/copyfiles from clickonce location to backup folder
                                File.Delete(s);
                            }                       
                        }
                    }        



    Thanks,

    Gayatri 

    • Edited by Gayatri KD Thursday, February 21, 2019 6:57 AM
    Thursday, February 21, 2019 6:55 AM
  • Hi Gayatri KD,

    Have you tried the code I provided before? It can implement the backup of folders. If an error occurs, it may be caused by a long path name, you can try to back up the folder under another path.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, February 21, 2019 8:33 AM
  • Hi Kyle,

    Yes, It is giving long path name,creating folder inside another folder.

    Even if multiple folders are created,I want only files to be copied to the backup folder,I do not to copy folders.

    I used the following code to copy only files.

      public void directoryCopy(string sourceDirectory, string targetDirectory)
            {
                try
                {
                    DirectoryInfo dir = new DirectoryInfo(sourceDirectory);
                    // Get files and subdirectories under the directory (without subdirectories)
                    FileSystemInfo[] fileinfo = dir.GetFileSystemInfos();
                    if (!Directory.Exists(targetDirectory + "\\"))
                    {
                        // Create a subfolder without this folder in the target directory
                        Directory.CreateDirectory(targetDirectory + "\\");
                    }
    
                    foreach (FileSystemInfo i in fileinfo)
                    {
                        if (i is FileInfo)
                        {
                            // Copy file if is not a folder, true means that files with the same name can be overwritten
                            File.Copy(i.FullName, targetDirectory + "\\" + i.Name);
                           
                        }
                        
                    }
                        
                }
                catch (Exception ex)
                {
                   MessageBox.Show(ex.Message);
                }
            }

    Thanks a lot,

    Gayatri



    • Edited by Gayatri KD Thursday, February 21, 2019 12:24 PM
    • Marked as answer by Gayatri KD Thursday, February 21, 2019 12:26 PM
    Thursday, February 21, 2019 10:46 AM
  • Hello,

    There is one trick to have the folder created under application folder at installation time.

    1. Add new folder - Backup - to your project.

    2. Add new file, any type, I use .txt with description of folder purpose, into this folder

    3. Set in file properties - Build Action = None, Copy to Output Directory = Copy Always.

    That's all - none of the code needed - installer will create a folder and will place a file into it.


    Sincerely, Highly skilled coding monkey.

    Thursday, February 21, 2019 10:58 AM
  • Hi Andrey,

    I want the files to be copied automatically through program,I do not want to copy manually as those files will be kept in server and every user has to copy those files on button click.

    Thanks,

    Gayatri

    Thursday, February 21, 2019 11:23 AM
  • Your original title on the topic is - Unable to create folder on clickonce location... 

    >I do not want to copy manually
    Do you see anywhere in my post request to do something manually on installation? 

    >every user has to copy those files on button click
    Only if you want this to be done on the button click. I didn't ask users learn which button to click or to do anything except what is their job. 


    Sincerely, Highly skilled coding monkey.

    Thursday, February 21, 2019 1:08 PM