History

History of .net

Thursday, 24 January 2013


Method to Add a Scheduler Dynamically through C# Code


Bfore writing this method we have to add a dll(Microsoft.Win32.TaskScheduler.dll) reference to our Application MUST
private bool CreateScheduler(string _newServicePath)
{
try
{
using (TaskService ts = new TaskService())
{
// Create a new task definition and assign properties
TaskDefinition td = ts.NewTask();
td.RegistrationInfo.Description = "Data Import Utility for " + txtShortCode.Text;
// Create a trigger that will fire the task at this time every other day
var tt = new TimeTrigger();
tt.Repetition.Interval = TimeSpan.FromMinutes(30);
td.Triggers.Add(tt);
// Create an action that will launch Notepad whenever the trigger fires
td.Actions.Add(new ExecAction(_newServicePath, null, null));
// Register the task in the root folder
ts.RootFolder.RegisterTaskDefinition(txtShortCode.Text, td);
return true;
}
}
catch (Exception ex)
{
ex.ToString();
return false;
}
}

Method to Replace a connection string in Appconfig of a Application


private bool Replaceconnectionstr(string filepath, string KeyCode, string NewConnectionString)
{
try
{
string otherExePath = @filepath;
Configuration otherConfig = ConfigurationManager.OpenExeConfiguration(otherExePath);
string otherSetting = otherConfig.AppSettings.Settings[KeyCode].Value;
otherConfig.AppSettings.Settings[KeyCode].Value = NewConnectionString;
otherConfig.Save();
return true;
}
catch (Exception ex)
{
ex.ToString();
return false;
}
}

Method to copy files and folder from one Folder to another Folder

private bool CopyFolderContents(string SourcePath, string DestinationPath)
{
SourcePath = SourcePath.EndsWith(@"\") ? SourcePath : SourcePath + @"\"; DestinationPath = DestinationPath.EndsWith(@"\") ? DestinationPath : DestinationPath + @"\";

try
{
if (Directory.Exists(SourcePath))
{
if (Directory.Exists(DestinationPath) == false)
{
Directory.CreateDirectory(DestinationPath);
}

foreach (string files in Directory.GetFiles(SourcePath))
{
FileInfo fileInfo = new FileInfo(files);
fileInfo.CopyTo(string.Format(@"{0}\{1}", DestinationPath, fileInfo.Name), true);
}

foreach (string drs in Directory.GetDirectories(SourcePath))
{
DirectoryInfo directoryInfo = new DirectoryInfo(drs);
if (CopyFolderContents(drs, DestinationPath + directoryInfo.Name) == false)
{
return false;
}
}
}
return true;
}
catch (Exception ex)
{
return false;
}
}

Wednesday, 23 January 2013

To get file Extension through file upload control

To get the extension of a uploaded file through file upload control. Path.GetExtension(FileUpload1.FileName)

Monday, 7 January 2013


How to redirect to another page after alert message?


Through HTML
Response.Write("");

Method through C# Code ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ScriptKey", "alert('Cancel Clicked');window.location='NewMetricAddition.aspx'; ", true);

To break the label text in Grid or any Datacontrol

<asp:Label ID="lblBenchMarkFormula" style="word-break:break-all;" runat="server" Text='<%# Eval("BenchMarkFormula") %>' Width="160px"></asp:Label>