That’s pretty easy stuff if you know it already.
Take a look on example (TestApp.exe):
static int Main(string[] args)
{
int myCodeNumber = 1;
// Return some code number
return myCodeNumber;
}
and here it is how to catch it in Windows batch file (test.bat):
REM Run app which will return some code number
TestApp.exe
REM If returned code is not == 1 then "do something" else continue to NEXT section
if %errorlevel% NEQ 1 GOTO :NEXT
DO SOMETHING HERE
:NEXT
Snippet:
Assembly assembly = Assembly.GetExecutingAssembly();
string filePath = assembly.GetName().Name + ".filename.xml";
using (Stream stream = assembly.GetManifestResourceStream(filePath))
{
if (stream != null)
{
// do something with Stream from file
}
}
Very often InnerException of Exception has some valuable details what happens. Quite often InnerException has an InnerException so more error messages to investigate.
Theoretically there can be unlimited number of Inner Exceptions so below are 2 small snippets to get all messages from them. Both snippets do the same but first in a “while method”, second in recursive way.
/// <summary>
/// Gets all inner exceptions messages.
/// </summary>
/// <param name="ex">The exception</param>
/// <returns>Messages from all inner exceptions.</returns>
public static string GetInnerExceptionMessages(Exception ex)
{
Exception inner = ex.InnerException;
string messages = String.Empty;
while (inner != null)
{
messages += inner.Message;
if (!messages.EndsWith("."))
{
messages += ".";
}
inner = inner.InnerException;
}
return messages;
}
And another method as recursive function from:
http://stackoverflow.com
public string GetInnerException(Exception ex)
{
if (ex.InnerException != null)
{
return string.Format("{0} > {1} ", ex.InnerException.Message,GetInnerException(ex.InnerException));
}
return string.Empty;
}
private const int WS_HSCROLL = 0x100000;
private const int WS_VSCROLL = 0x200000;
private const int GWL_STYLE = (-16);
[System.Runtime.InteropServices.DllImport("user32",
CharSet = System.Runtime.InteropServices.CharSet.Auto)]
private static extern int GetWindowLong(IntPtr hwnd, int nIndex);
internal static bool IsVerticalScrollBarVisible(Control ctrl)
{
if (!ctrl.IsHandleCreated)
return false;
return (GetWindowLong(ctrl.Handle, GWL_STYLE) & WS_VSCROLL) != 0;
}
internal static bool IsHorizontalScrollBarVisible(Control ctrl)
{
if (!ctrl.IsHandleCreated)
return false;
return (GetWindowLong(ctrl.Handle, GWL_STYLE) & WS_HSCROLL) != 0;
}
Usage example:
if (IsVerticalScrollBarVisible(ListView1))
DoSomething;
//For example, make a string with 32 “a” chars.
string test = new string('a', 32 );
To insert into database NULL value from C# code we can’t use just simple declaration for empty string like string textToInsert = “”; This will insert into database just empty string instead NULL value.
To handle this case you have to use special SQL type: System.Data.SqlTypes.SqlString.Null
Dummy example (do nothing special but shows the way) below:
using (SqlConnection cn = new SqlConnection(connectionString))
{
string sqlText = "stored_procedure_name";
using (SqlCommand cmd = new SqlCommand(sqlText, cn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@TaskType", SqlDbType.NVarChar, 255).Value = System.Data.SqlTypes.SqlString.Null;
try
{
cn.Open();
cmd.ExecuteNonQuery();
}
catch (SqlException sqlEx)
{
}
}
}
System.Data.SqlTypes namespace has all SQL Server types so if you need to insert NULL value then use suitable one for your column data type.
Source:
http://stackoverflow.com/questions/38039/how-can-i-get-the-datetime-for-the-start-of-the-week
If you have similiar solution than test it for 2009-02-01 (sunday) date (yyyy-mm-dd). Correct result should be 2009-01-26.
Extension method:
public static class DateTimeExtensions
{
public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)
{
int diff = dt.DayOfWeek - startOfWeek;
if (diff < 0)
{
diff += 7;
}
return dt.AddDays(-1 * diff).Date;
}
}
Example of use
DateTime dt = DateTime.Now.StartOfWeek(DayOfWeek.Monday);
DateTime dt = DateTime.Now.StartOfWeek(DayOfWeek.Sunday);