I have a C# application, it works properly on many pc, laptop. But, I copied to my customer's pc (4TB HDD - windows 10 Home Edition), my application stop working!
I try to put MessageBox.Show()
in some line to find where is broken. And it stop at Directory.CreateDirectory(@"D:\\mypath")
The PC have D:
and I don't know why it broken.
Here is my code:
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);
How can I trace exactly my errors, and is there anything wrong from my code? It running perfectly in many PCs but with this PC, it broken.
Is my problem related to large hard drive space?
My customer's IT staff installed my app on his laptop (Windows 10 Home) and installed the same windows to this pc. My app run on His laptop without errors
Thanks you!
EDIT: My Function and my errors:
Function:
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);
}
Call function
ThreadStart childref = new ThreadStart(() => makevideo());
Thread childThread = new Thread(childref);
try { childThread.Start(); }
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
Errors: **
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()
**
I don't usually recommend catching errors like this
However you can use a logger, or if you really must you can just push the error into a MessageBox
. at least you will know the exception
Alternatively you could check the event log viewer, if your application crashes it will give you clues as to what happened.
Lastly, most likely this is a permission thing, but who knows. Make sure your client has given the appropriate permissions to that directory or run your application at an elevated privilege
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)
Exceptions
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:\").