An *_*ham 4 c# create-directory
我有一个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);
Run Code Online (Sandbox Code Playgroud)
如何准确追踪我的错误,我的代码有什么问题吗?它在许多PC上运行得很好但是有了这台PC,它就坏了.
我的问题与大硬盘空间有关吗?
我的客户的IT人员在我的笔记本电脑(Windows 10 Home)上安装了我的应用程序,并在此电脑上安装了相同的窗口.我的应用在他的笔记本电脑上运行没有错
谢谢!
编辑: 我的功能和我的错误:
功能:
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);
}
Run Code Online (Sandbox Code Playgroud)
通话功能
ThreadStart childref = new ThreadStart(() => makevideo());
Thread childThread = new Thread(childref);
try { childThread.Start(); }
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
Run Code Online (Sandbox Code Playgroud)
错误: **
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()
Run Code Online (Sandbox Code Playgroud)
**
我通常不建议捕捉这样的错误
但是,您可以使用记录器,或者如果您真的必须,您可以将错误推送到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)
}
Run Code Online (Sandbox Code Playgroud)
Directory.CreateDirectory方法(String)
例外
IOException异常
- path指定的目录是一个文件.
- 网络名称未知.
UnauthorizedAccessException
- 呼叫者没有所需的权限.
ArgumentException的
path是零长度字符串,仅包含空格,或包含一个或多个无效字符.您可以使用GetInvalidPathChars方法查询无效字符.
path以前缀为或仅包含冒号字符(:).
ArgumentNullException
- path为null.
PathTooLongException
- 指定的路径,文件名或两者都超过系统定义的最大长度.例如,在基于Windows的平台上,路径必须少于248个字符,文件名必须少于260个字符.
DirectoryNotFoundException
- 指定的路径无效(例如,它位于未映射的驱动器上).
NotSupportedException异常
- path包含冒号字符(:),它不是驱动器标签("C:\")的一部分.
| 归档时间: |
|
| 查看次数: |
295 次 |
| 最近记录: |