简体   繁体   English

在部署目标上创建文件夹/目录

[英]Folder/Directory Creation on Deployment Targets

I am deploying a small script to the target PC using Octopus Deploy but the script is not generating a new folder on the target machine. 我正在使用Octopus Deploy将小的脚本部署到目标PC,但是该脚本未在目标计算机上生成新文件夹。 Is there a problem in my code or is it a problem with the deployment setup? 我的代码有问题还是部署设置有问题?

using System;
using System.IO;

class FolderCreation
{
    public static void Main()
    {
        string _basePath = @"c:\OD_Test";
        try
        {
            string dateFolder = DateTime.Now.ToString("yyyy-MM-dd");
            string path = $"{_basePath}\\{dateFolder}";

            System.IO.Directory.CreateDirectory(path);
        }
        catch (Exception ex)
        {
             Console.WriteLine("The process failed: {0}", ex.ToString());
        }
    }
}

First of all, there can be many possible issues in the code. 首先,代码中可能存在许多可能的问题。 Ideally when you post, please post the details of the exception. 理想情况下,发布时,请发布例外的详细信息。

I suspect two things: - your parent directories are not present and that's why your create directory is failing. 我怀疑两件事:-您的父目录不存在,这就是您的创建目录失败的原因。 - the code does not have permission to create directory in the existing path. -该代码无权在现有路径中创建目录。

You can know this by the exception type which is get in the catch block. 您可以通过catch块中获取的异常类型知道这一点。 Directory.CreateDirectory throws below exceptions in case it is not successful: 如果失败,Directory.CreateDirectory会抛出以下异常:

  • IOException - The directory specified by path is a file. IOException -path指定的目录是一个文件。

  • UnauthorizedAccessException - The caller does not have the required permission. UnauthorizedAccessException-调用者没有所需的权限。

  • ArgumentException - path is a zero-length string, contains only white space, or contains one or more invalid characters. ArgumentException-路径是长度为零的字符串,仅包含空格,或包含一个或多个无效字符。 You can query for invalid characters by using the GetInvalidPathChars() method. 您可以使用GetInvalidPathChars()方法查询无效字符。 OR path is prefixed with, or contains, only a colon character (:). OR路径仅以冒号(:)开头或包含。

  • ArgumentNullException - path is null. ArgumentNullException-路径为null。

  • PathTooLongException - The specified path, file name, or both exceed the system-defined maximum length. PathTooLongException-指定的路径,文件名或两者都超过系统定义的最大长度。

  • DirectoryNotFoundException - The specified path is invalid (for example, it is on an unmapped drive). DirectoryNotFoundException-指定的路径无效(例如,它在未映射的驱动器上)。

  • NotSupportedException - path contains a colon character (:) that is not part of a drive label ("C:\\"). NotSupportedException-路径包含一个不包含在驱动器标签(“ C:\\”)中的冒号(:)。

NOTE: Details of exceptions thrown by CreateDirectory is taken from documentation at MSDN . 注意:CreateDirectory引发的异常的详细信息取自MSDN上的文档

Octopus Deploy only allows script files to be accepted. Octopus Deploy仅允许接受脚本文件。 I was NOT supposed to create a class itself but only include the information inside the main method. 我不应该自己创建类,而只能在main方法中包含信息。 Functions can be added by using OD's script modules. 可以使用OD的脚本模块添加功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM