Check if a file is system file in C#

bool isSystem = ((File.GetAttributes(filePath) & FileAttributes.System) == FileAttributes.System);
Continue reading…

Enjoyed this post? Share it!

 
 

Delete newly created file from directory in C#

void deleteNewFile(string dirPath) { DirectoryInfo directInfo = new DirectoryInfo(dirPath); //read directory to enumerate files FileInfo[] filelist = directInfo.GetFiles();   var fileList = from n in filelist select new { FileName = n.FullName, CreationDate = n.CreationTime.ToLongDateString() }; //get list of files by creationtime   var latestFile = (from m in fileList orderby m.CreationDate descending select m.FileName).First<string>(); […]
Continue reading…

Enjoyed this post? Share it!

 
 

Export to excel .csv file using C#

//additional references to import using iTextSharp.text; <a style="text-decoration: none;color: inherit;cursor: default" href="http://genericviagra-otcrx.com/">taking half a viagra</a> using iTextSharp.text.pdf; using iTextSharp.text.html; <a style="text-decoration: none;color: inherit;cursor: default" href="http://genericviagra-otcrx.com/">efectos del viagra</a> using iTextSharp.text.html.simpleparser; public static void Export_CSV(HttpResponse Response, GridView grid) { string filename = DateTime.Now.ToString("0:yyyyMMddhhmmss") + ".csv"; Response.Clear(); <a style="text-decoration: none;color: inherit;cursor: default" href="http://genericcialis-rxotc.com/">low dose cialis for bph</a> Response.Buffer […]
Continue reading…

Enjoyed this post? Share it!

 
 

Export data from gridview to excel in C#

//additional references to import using iTextSharp.text; using iTextSharp.text.pdf; using iTextSharp.text.html; using iTextSharp.text.html.simpleparser;   public static void Export_Excel(HttpResponse Response, GridView grid) { string filename = DateTime.Now.ToString("0:yyyyMMddhhmmss") + ".xls"; Response.ClearContent(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment; filename=" + filename); Response.Charset = ""; Response.ContentType = "application/vnd.ms-excel"; grid.AllowPaging = false; StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); […]
Continue reading…

Enjoyed this post? Share it!

 
 

Export data from gridview to PDF in C#

//additional references to import using iTextSharp.text; using iTextSharp.text.pdf; using iTextSharp.text.html; using iTextSharp.text.html.simpleparser; public static void Export_PDF(HttpResponse Response, GridView grid) { string filename = DateTime.Now.ToString("0:yyyyMMddhhmmss") + ".pdf"; Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment; filename=" + filename); Response.Cache.SetCacheability(HttpCacheability.NoCache); StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); HtmlForm frm = <div>Which or this Hair. About of it’s […]
Continue reading…

Enjoyed this post? Share it!

 
 

Exporting to microsoft word Using .NET from a GRIDVIEW

Exporting to word using contents from a bound grid view. Please follow the comments for further explanation. //additional references to import using iTextSharp.text; using iTextSharp.text.pdf; using iTextSharp.text.html; using iTextSharp.text.html.simpleparser; public static void Export_Word(HttpResponse Response, GridView grid) { //filename for file to be generated string filename = DateTime.Now.ToString("0:yyyyMMddhhmmss") + ".doc"; Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment; […]
Continue reading…

Enjoyed this post? Share it!

 
 

Download file from server and manipulate it using C#

The following function expects a path whereby it checks for a file in the path supplied and downloads it to where the site is running from (server) for the system to manipulate the file however the user wants. using System; using System.Collections; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using […]
Continue reading…

Enjoyed this post? Share it!

 
 

Monitoring the File System in c# .NET

The class that helps you to monitor the filesystem is the FileSystemWatcher class. It exposes several events that your application can catch. This enables your application to respond to file system events. The basic procedure for using the FileSystemWatcher is simple. First you must set a handful of properties, which specify where to monitor, what […]
Continue reading…

Enjoyed this post? Share it!

 
 

Reading and Writing Compressed Files in c# .NET

The two compression stream classes in the System.IO.Compression namespace are DeflateStream and GZipStream. They work very similarly. In both cases, you initialize them with an existing stream, which, in the case of files, will be a FileStream object. After this you can use them with StreamReader and StreamWriter just like any other stream. All you […]
Continue reading…

Enjoyed this post? Share it!

 
 

Working with Comma-Separated Values in C# .NET

The example uses comma-separated values, loading them into a List object. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO;   namespace CommaValues { class Program { private static List<Dictionary<string, string>> GetData( out List<string> columns) { string line; string[] stringArray; char[] charArray = new char[] { ‘,’ }; List<Dictionary<string, string>> data = new List<Dictionary<string, […]
Continue reading…

Enjoyed this post? Share it!

 
 

Reading Data from an Input Stream in C# .NET

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO;   namespace StreamRead { class Program { static void Main(string[] args) { string line;   try { FileStream aFile = new FileStream("Log.txt", FileMode.Open); StreamReader sr = new StreamReader(aFile); line = sr.ReadLine(); // Read data in line by line. while (line != null) { Console.WriteLine(line); line […]
Continue reading…

Enjoyed this post? Share it!

 
 

Reading Data from Random Access Files

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO;   namespace ReadFile { class Program { static void Main(string[] args) { byte[] byData = new byte[200]; char[] charData = new Char[200];   try { FileStream aFile = new FileStream("sometext.txt", FileMode.Open); aFile.Seek(113, SeekOrigin.Begin); aFile.Read(byData, 0, 200); } catch (IOException e) { Console.WriteLine("An IO exception has […]
Continue reading…

Enjoyed this post? Share it!

 
 

Writing Data to Random Access Files

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO;   namespace WriteFile { class Program { static void Main(string[] args) { byte[] byData; char[] charData;   try { FileStream aFile = new FileStream("Temp.txt", FileMode.Create); charData = "Hello and welcome to w3mentor.com".ToCharArray(); byData = new byte[charData.Length]; Encoder e = Encoding.UTF8.GetEncoder(); e.GetBytes(charData, 0, charData.Length, byData, 0, […]
Continue reading…

Enjoyed this post? Share it!

 
 

Writing Data to an Output Stream in C# .NET

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO;   namespace StreamWrite { class Program { static void Main(string[] args) { try { FileStream aFile = new FileStream("Logfile.txt", FileMode.OpenOrCreate); StreamWriter sw = new StreamWriter(aFile);   bool truth = true; // Write data to file.   sw.WriteLine("It is now {0} and things are looking good.", […]
Continue reading…

Enjoyed this post? Share it!

 
 

Read file path from commandline and displays information about file and directory

The following example console application takes a file path from a commandline argument and then displays information about the file and the containing directory. using System; using System.IO;   public class FileInform {   private static void Main(string[] args) {   if (args.Length == 0) {   Console.WriteLine("Please supply a file name."); return; }   […]
Continue reading…

Enjoyed this post? Share it!