Răzvan Cosma's scratchpad

random scripts of random value

Browsing Posts in C#

$word = New-Object -ComObject word.application $word.visible = $false $pages = 0 try { Get-ChildItem -path “.” | ? {$_.name -match “.*\.docx{0,1}$”} | foreach-object { write-host $_.name # fullName, confirmConversion, readOnly, addToRecent, passwordDocument $doc = $word.documents.open($_.fullname, $false, $true, $false, “”) $window = $doc.ActiveWindow; $panes = $window.Panes; $pane = $Panes.item(1) $pages += $pane.pages.count $doc.close() write-host $pages } […]

private static CacheItemRemovedCallback OnCacheRemove = null; protected void Application_Start(object sender, EventArgs e) { AddTask(“DoStuff”, 60); } private void AddTask(string name, int seconds) { OnCacheRemove = new CacheItemRemovedCallback(CacheItemRemoved); HttpRuntime.Cache.Insert(name, seconds, null, DateTime.Now.AddSeconds(seconds), Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, OnCacheRemove); } public void CacheItemRemoved(string k, object v, CacheItemRemovedReason r) { // do stuff here if it matches our taskname, like WebRequest […]

[WebService(Namespace = “http://server”)] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class myservice : WebService { [WebMethod] public string someq(string var1, string var2) { return var1+var2; } <?php $wsdl = “http://server/service.asmx?wsdl”; $client = new SoapClient($wsdl); var_dump($client->someq(array(‘var1’=>”val1″, ‘var2’=>”val2″))); ?>

There is no ready-made class, so you’ll have to call the Windows API [DllImport(“KERNEL32.DLL”, EntryPoint = “GetPrivateProfileStringW”, SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] private static extern int GetPrivateProfileString( string lpAppName, string lpKeyName, string lpDefault, string lpReturnString, int nSize, string lpFilename); [DllImport(“KERNEL32.DLL”, EntryPoint = “WritePrivateProfileStringW”, SetLastError = true, CharSet = […]

ClientScript.RegisterClientScriptBlock(this.GetType(), “closePg”, “window.open(”, ‘_self’, ”);window.close();”, true);

In web.config: <configuration> … <connectionStrings> <add name=”defaultconn” connectionString=”Data Source=server.domain.com; Initial Catalog=thedatabase; Persist Security Info=True; User ID=theuser; Password=thepass” providerName=”.NET Framework Data Provider for SQL Server”/> </connectionStrings> … </configuration> In App_Code: using System.Data.SqlClient; namespace whatever { public static DataTable GetTable(string connstr, string commstr, params object[] parameters) { if (commstr == null) return null; if (connstr == null) […]

private void ExportToExcel(string strFileName, DataGrid dg) { Response.ClearContent(); Response.ContentType = “application/excel”; Response.AddHeader(“content-disposition”, “attachment; filename=” + strFileName); Response.Charset = “”; Response.ContentEncoding = System.Text.Encoding.Unicode; Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble()); this.EnableViewState = false; System.IO.StringWriter sw = new System.IO.StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); dg.RenderControl(htw); Response.Write(sw.ToString()); Response.End(); }

1. In web.config: <configuration> … <appSettings> … <add key=”connStr” value=”server=server_name_or_ipaddress; Initial Catalog=database_name; User Id=username; Password=password;” /> … </appSettings> … </configuration> 2. In your .aspx file: <asp:Repeater ID=”rep1″ runat=”server”> <HeaderTemplate><table border=”1″></HeaderTemplate> <ItemTemplate> <tr> <td><%# DataBinder.Eval(Container.DataItem, “col1”) %></td> <td><%# DataBinder.Eval(Container.DataItem, “col2”) %></td> </tr> </ItemTemplate> <FooterTemplate></table></FooterTemplate> </asp:Repeater> 3. In your .aspx.cs file: using System.Data; using System.Data.SqlClient; protected Repeater […]

Bad Behavior has blocked 69 access attempts in the last 7 days.