C# Directory.CreateDirectory 停止工作

C# Directory.CreateDirectory stop working

我有一个 C# 应用程序,它在许多 pc、笔记本电脑上都能正常工作。但是,我复制到我客户的电脑(4TB 硬盘 - windows 10 家庭版),我的应用程序停止工作!

我尝试将 MessageBox.Show() 放在某行中以查找损坏的地方。它停在 Directory.CreateDirectory(@"D:\mypath")

PC有D:不知道为什么坏了

这是我的代码:

string now = DateTime.Now.ToString("HH_mm_ss");
string strDuongDan;

strDuongDan = @"D:\VideoLuuTru\" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Year.ToString();

if (!Directory.Exists(strDuongDan))
    Directory.CreateDirectory(strDuongDan);

string strDuongDan2 = strDuongDan + "\" + DateTime.Now.ToString("dd"); ;

if (!Directory.Exists(strDuongDan2))
    Directory.CreateDirectory(strDuongDan2);

我怎样才能准确地跟踪我的错误,我的代码有什么问题吗?它 运行 在许多 PC 上运行完美,但在这台 PC 上,它坏了。

我的问题是否与大硬盘有关space?

我客户的 IT 人员在他的笔记本电脑(Windows 10 Home)上安装了我的应用程序,并在这台电脑上安装了相同的 windows。我的应用程序 运行 在他的笔记本电脑上没有错误

谢谢!

编辑: 我的功能和我的错误:

函数:

 public void makevideo()
        {
            string now = DateTime.Now.ToString("HH_mm_ss");
            string strDuongDan;

            strDuongDan = @"D:\VideoLuuTru\" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Year.ToString();

            if (!Directory.Exists(strDuongDan))
                Directory.CreateDirectory(strDuongDan);

            string strDuongDan2 = strDuongDan + "\" + DateTime.Now.ToString("dd"); ;
            if (!Directory.Exists(strDuongDan2))
                Directory.CreateDirectory(strDuongDan2);
        }

调用函数

 ThreadStart childref = new ThreadStart(() => makevideo());

 Thread childThread = new Thread(childref);

 try { childThread.Start(); }
 catch (Exception ex)
 {
     MessageBox.Show(ex.ToString());
  }

错误: **

Application: camera.exe Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. 
Exception Info: System.IO.FileNotFoundException at camera.Form1.makevideo() at camera.Form1.<Form1_Load>b__6_0() at         System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext,
System.Threading.ContextCallback, System.Object, Boolean) at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, 
System.Threading.ContextCallback, System.Object) at 
System.Threading.ThreadHelper.ThreadStart()

**

我通常不建议捕获这样的错误

但是您可以使用记录器,或者如果您真的必须将错误推送到 MessageBox。至少你会知道例外

或者您可以检查事件日志查看器,如果您的应用程序崩溃,它会为您提供有关发生了什么的线索。

最后,这很可能是许可问题,但谁知道呢。确保您的客户端已授予对该目录的适当权限,或者 运行 您的应用程序处于提升的权限

try
{
    // Note you don't need to check if a directory exists before you create it
    // it does it for you
    // if (!Directory.Exists(strDuongDan))
    Directory.CreateDirectory(strDuongDan);
}
catch(Exception ex)
{
   // log here
   // or
   MessageBox.Show("Error : " + ex.Message)
}

Directory.CreateDirectory Method (String)

异常

  • IOException

    • The directory specified by path is a file.
    • The network name is not known.
  • UnauthorizedAccessException

    • The caller does not have the required permission.
  • ArgumentException

    • path is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the GetInvalidPathChars method.

    • path is prefixed with, or contains, only a colon character (:).

  • ArgumentNullException

    • path is null.
  • PathTooLongException

    • The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters and file names must be less than 260 characters.
  • DirectoryNotFoundException

    • The specified path is invalid (for example, it is on an unmapped drive).
  • NotSupportedException

    • path contains a colon character (:) that is not part of a drive label ("C:\").