gpt4 book ai didi

C# Directory.CreateDirectory 停止工作

转载 作者:太空狗 更新时间:2023-10-30 00:48:07 26 4
gpt4 key购买 nike

我有一个 C# 应用程序,它可以在许多 PC、笔记本电脑上正常运行。但是,我复制到我客户的电脑(4TB HDD - 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 上,它坏了。

我的问题与大硬盘空间有关吗?

我客户的 IT 人员在他的笔记本电脑(Windows 10 家庭版)上安装了我的应用程序,并在这台电脑上安装了相同的 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 中。至少你会知道异常(exception)情况

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

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

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)

异常(exception)情况

  • 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:\").

关于C# Directory.CreateDirectory 停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49606504/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com