Open file from browser

Response.ContentType = “application/pdf”;

Response.AddHeader(“content-disposition”, “attachment; filename=” + path);

Response.WriteFile(path);

HttpContext.Current.ApplicationInstance.CompleteRequest();

 

Note. Comment second line to hide download file dialog OR change with this line

Response.AddHeader(“content-disposition”, “inline; filename=” + directory + pathFile);

Anuncios

Check if file path is inside a directory

private bool IsInside(DirectoryInfo path, DirectoryInfo folder){

if(path.Parent == null)

{ return false; }

if(String.Equals(path.Parent.FullName, folder.FullName, stringComparison.InvariantCultureIgnoreCase))

{ return true; }

return IsInside(path.Parent, folder);

}

DirectoryInfo folder = new DirectoryInfo(“C:\\Users\\Folder”);

DirectoryInfo path = new DirectoryInfo(“C”\\Users\\Folder\\file.txt”);

bool result = IsInside(path, folder);

Upload file to FTP

try{

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(“C://…/text.txt”);

request.Method = WebRequestMethods.Ftp.UploadFile;

request.Credentials = new NetworkCredential(“username”, “password”);

request.GetResponse();

}catch(WebException ex){

MessageBox.Show(ex.ToString());

}

Save RichTextBox to a file

richTextBox1.SaveFile(@”C:\…\test.txt”, RichTextBoxStreamType.PlainText);

Save dialog

SaveFileDialog dlg = new SaveFileDialog();

dlg.FileName = “file name”;

dlg.DefaultExt = “.pdf”;

dlg.Filter = “Text documents (.pdf) | *.pdf”;

if(dlg.ShowDialog() == DialogResult.OK){

string Pdfname = dlg.FileName;

Process.Start(Pdfname);

}