listing files using C#, file with % wont open - c#

I am listing pdf files using C#, but some files wont open because they have percentage(%) signs on their filenames, the user still wants the % to be shown on the filename, but I can't get it to work.
DirectoryInfo directory = new DirectoryInfo("mydirectory/News Files");
FileSystemInfo[] files = directory.GetFiles("*.pdf");
var orderedFiles = files.OrderByDescending(f => f.Name);
foreach (FileSystemInfo file in orderedFiles)
{
var link = new HyperLink { ID = file.FullName };
link.NavigateUrl ="/News Files/"+ file.Name;
link.Text = Regex.Split(file.Name, ".pdf")[0];
link.CssClass = "linkpdf";
newsListContainer.Controls.Add(link);
}
But with this code file with the name like my20%sign.pdf will not open in the browser.

You could try Uri.EscapeUriString.
Also, you shouldn't construct urls/filenames using string concatenation with /. You should usually use a Uri/filename parsing library, such as the Uri class

That's not surprising. The %20 is interpreted by the browser as a "white space", as it is the url encoded equivalent value. So if your file is named "My%20File.pdf", the browser will decode the url and actually look for "My File.pdf".
For further reference, check this: http://www.w3schools.com/tags/ref_urlencode.asp

You can replace %20 with " ". filename.replace("%20"," ");

Related

C#: Saving iText7 PDFs into a folder chosen by the user with a dialog

I would make a simple program in C# with Windows forms, which gets some data given by the user thanks to some textboxes, and when He presses a button, a dialog (I don't know which one) is displayed, in order to explore the pc folders and choose a destination for saving it there.
Well, I used a FolderBrowserDialog (I don't know if that's the right one for the purpose), but there's a problem: in order to store a PDF with itext7, I have to give an Environment.SpecialFolder variable, while the method selectedPath() to get the user path of the formBrowserDialog returns a string.
I tried to convert the string into Environment.SpecialFolder in some way, but I always get a System.ArgumentException
Here's my code:
string name = txtName.Text;
//
//bla bla bla getting the parameters given by the user
//...
string pdfName = surname+ " - " + hours + "ː" + minutes + ".pdf";
string folder="";
//"fbd" is the FolderBrowserDialog
if (fbd.ShowDialog() == DialogResult.OK)
{
//here I get the folder path (I hope I've chosen the right dialog for this scope, which is a FolderBrowserDialog)
folder = fbd.SelectedPath;
//starting my pdf generation
//here is my attempt to write something in order to parse the path string into an Environment.SpecialFolder type, to use it as a parameter in getFolderPath()
Environment.SpecialFolder path = (Environment.SpecialFolder)Enum.Parse(typeof(Environment.SpecialFolder), folder);
//here it's supposed to give to the GetFolderPath method the Environment.SpecialFolder type.
var exportFolder = Environment.GetFolderPath(path); //ON THIS LINE I GET THE EXCEPTION
var exportFile = System.IO.Path.Combine(exportFolder, pdfName);
using (var writer = new PdfWriter(exportFile))
{
using (var pdf = new PdfDocument(writer))
{
var doc = new Document(pdf);
doc.Add(new Paragraph("
//bla bla bla writing my things on it
"));
}
}
//pdf creation ends
}
To simplify all of this, you don't need to Environment.SpecialFolder variable at all, nor do you need to pass it as a parameter.
The reason that an exception was thrown is because you tried to parse a string into an Environment.SpecialFolder variable enum, when the string could not be parsed into one.
You can look here to see the list of enums included. I would wager that the specific path you selected matches none of those.
Here's what your code is currently doing:
Selecting a path
Trying to parse that path to get an enum for a
special folder
Trying to get the string associated with that
Environment.SpecialFolder variable (so if you had actually been
able to parse it, you would've ended up with just what you started
with)
Combining that string with the name you wanted to give the PDF.
You can simplify all of this by omitting steps 2 and 3, which cause the error.
string pdfName = surname+ " - " + hours + "ː" + minutes + ".pdf";
//You select the folder here
if (fbd.ShowDialog() == DialogResult.OK)
{
string folder = fbd.SelectedPath;
//combine the path of the folder with the pdf name
string exportFile = System.IO.Path.Combine(folder, pdfName);
using (var writer = new PdfWriter(exportFile))
{
using (var pdf = new PdfDocument(writer))
{
var doc = new Document(pdf);
doc.Add(new Paragraph("//bla bla bla writing my things on it"));
}
}
//Pdf creation ends
}

C# Unable to unzip files zipped with CreateEntryFromFile

As per this post I'm zipping SQL backup files with the code below:
using (ZipArchive zip = ZipFile.Open("test.bak", ZipArchiveMode.Create))
{
zip.CreateEntryFromFile(#"c:\something.txt", "data/path/something.txt");
}
The zip file are created successfully with the correct file size. But I can't unzip the file. I get the the error:
The compressed (zip) folder is invalid or corrupted
I've tried using 7-zip and the build-in Windows 'Extract All' options.
I also tried reinstalling the software with no luck.
My version of the code:
var fileName = Path.GetFileName(e.FullPath);
var newFile = dir + "\\" + fileName + ".zip";
var backupFile = txtBackupFolder.Text == "" ? "" : txtBackupFolder.Text + "\\" + fileName + ".zip";
using (ZipArchive zip = ZipFile.Open(newFile, ZipArchiveMode.Create))
{
zip.CreateEntryFromFile(#e.FullPath, newFile);
}
Nkosi's post above did help, but I think what added to the problem was the escape characters in the newfile variable.
This does work:
using (ZipArchive zip = ZipFile.Open(newFile, ZipArchiveMode.Create))
{
zip.CreateEntryFromFile(#e.FullPath, "mybackup.bak");
}
I faced this very issue, and mine was related to special characters in the file names I was generating (specifically, a : character). So my code looked like
using (ZipArchive zip = ZipFile.Open(newFile, ZipArchiveMode.Create))
{
var title = GetTitleOfHtmlFile(); //code to get the file name to use
zip.CreateEntryFromFile(#e.FullPath, title); // title = "Chapter 1: My Chapter.html"
}
As the title being generated contained path-unfriendly characters, while I could add the entry to a zip file, when I tried to unzip the file, the OS would freak out because it couldn't deal with the path-unfriendly characters in the entry's name.
I simply changed the title to include an extension method to strip out such things, as so
title.ReplaceScreenUnfriendlyChars("-");
With an extension method like this:
public static string ReplaceScreenUnfriendlyChars(
this string curString,
string replacementString = "")
{
return string.Join(replacementString, curString.Split(Path.GetInvalidFileNameChars()));
}

Reverting Replace in a text file

I have a template I'm using to print labels, what I'm currently doing is a Replace() on the variable parts of my template and print it as is.
What is the best way to recover the original template after printing ? Revert manually all the changes ? Not modifying the template at first but create a copy that I modify, print and delete ?
The template looks like :
data1 : $1
data2 : $2
data3 : $3
data4 : $4
and then Replace() + print with :
string text = File.ReadAllText(filePath);
text = text.Replace("$1", textBoxNumOF.Text);
text = text.Replace("$2", designation);
text = text.Replace("$3", textBoxNumOF.Text.Substring(textBoxNumOF.Text.Length - 4));
text = text.Replace("$4", "1");
File.WriteAllText(filePath, text, UTF8Encoding.UTF8);
PrintDialog pd1 = new PrintDialog();
pd1.PrinterSettings = new PrinterSettings();
EnvoiImpression.SendFileToPrinter(#"Datamax-O'Neil H-4310 (Copie 1)", filePath);
Read your template and write the output which you are sending to the printer into a temp file inside the temp directory of windows.
Please see the following function:
public static string GetTempFile()
{
// get temporary path
var tempPath = Path.GetTempPath();
// get temporary filename
string tempFileName = Path.GetRandomFileName();
//combine
return Path.Combine(tempPath, tempFileName);
}
This way you do not need to revert your template and comply with the rules for temporary files on Windows. I suggest that you remember the files for deleting all your temporary files again from disk after your program / method was successful.
The function
EnvoiImpression.SendFileToPrinter(#"Datamax-O'Neil H-4310 (Copie 1)", filePath);
is sadly unknown to my. But perhaps there is also an overload which does accept a Stream? If so, you could edit your template in a MemoryStream and do not even need to write to the disk.

GetDirectories returns path\\dir

I am using string[] dirs = Directory.GetDirectories("instances", "*") to get all the directories.
But it returns the directories as followed:
instances\\test01, instances\\test02
Then i use the following function to download a file to that directory:
FileDownloader downloader = new FileDownloader(dirs[0] + "/server/server.jar", "blabla");
But the file appear in the root directory instead.Any suggestions?
Instead of dirs[0] + "/server/server.jar"
use
Path.Combine(dirs[0], "/server/server.jar")
I'd imagine that the string appending could be treating \t as a tab

How to find the extension of a file in C#?

In my web application (asp.net,c#) I am uploading video file in a page but I want to upload only flv videos. How can I restrict when I upload other extension videos?
Path.GetExtension
string myFilePath = #"C:\MyFile.txt";
string ext = Path.GetExtension(myFilePath);
// ext would be ".txt"
You may simply read the stream of a file
using (var target = new MemoryStream())
{
postedFile.InputStream.CopyTo(target);
var array = target.ToArray();
}
First 5/6 indexes will tell you the file type. In case of FLV its 70, 76, 86, 1, 5.
private static readonly byte[] FLV = { 70, 76, 86, 1, 5};
bool isAllowed = array.Take(5).SequenceEqual(FLV);
if isAllowed equals true then its FLV.
OR
Read the content of a file
var contentArray = target.GetBuffer();
var content = Encoding.ASCII.GetString(contentArray);
First two/three letters will tell you the file type.
In case of FLV its "FLV......"
content.StartsWith("FLV")
At the server you can check the MIME type, lookup flv mime type here or on google.
You should be checking that the mime type is
video/x-flv
If you were using a FileUpload in C# for instance, you could do
FileUpload.PostedFile.ContentType == "video/x-flv"
I'm not sure if this is what you want but:
Directory.GetFiles(#"c:\mydir", "*.flv");
Or:
Path.GetExtension(#"c:\test.flv")
In addition, if you have a FileInfo fi, you can simply do:
string ext = fi.Extension;
and it'll hold the extension of the file (note: it will include the ., so a result of the above could be: .jpg .txt, and so on....
string FileExtn = System.IO.Path.GetExtension(fpdDocument.PostedFile.FileName);
The above method works fine with the Firefox and IE: I am able to view all types of files like zip,txt,xls,xlsx,doc,docx,jpg,png.
But when I try to find the extension of file from Google Chrome, I fail.
EndsWith()
Found an alternate solution over at DotNetPerls that I liked better because it doesn't require you to specify a path. Here's an example where I populated an array with the help of a custom method
// This custom method takes a path
// and adds all files and folder names to the 'files' array
string[] files = Utilities.FileList("C:\", "");
// Then for each array item...
foreach (string f in files)
{
// Here is the important line I used to ommit .DLL files:
if (!f.EndsWith(".dll", StringComparison.Ordinal))
// then populated a listBox with the array contents
myListBox.Items.Add(f);
}
It is worth to mention how to remove the extension also in parallel with getting the extension:
var name = Path.GetFileNameWithoutExtension(fileFullName); // Get the name only
var extension = Path.GetExtension(fileFullName); // Get the extension only
You will not be able to restrict the file type that the user uploads at the client side[*]. You'll only be able to do this at the server side. If a user uploads an incorrect file you will only be able to recognise that once the file is uploaded uploaded. There is no reliable and safe way to stop a user uploading whatever file format they want.
[*] yes, you can do all kinds of clever stuff to detect the file extension before starting the upload, but don't rely on it. Someone will get around it and upload whatever they like sooner or later.
You can check .flv signature. You can download specification here:
http://www.adobe.com/devnet/flv/
See "The FLV header" chapter.
private string GetExtension(string attachment_name)
{
var index_point = attachment_name.IndexOf(".") + 1;
return attachment_name.Substring(index_point);
}
This solution also helps in cases of more than one extension like "Avishay.student.DB"
FileInfo FileInf = new FileInfo(filePath);
string strExtention = FileInf.Name.Replace(System.IO.Path.GetFileNameWithoutExtension(FileInf.Name), "");
Path.GetExtension(file.FileName)) will get you the file name
Im also sharing a test code if someone needs to test and ge the extention or name.
Forming a text file with name test.txt and checking its extention in xUnit.
[Fact]
public void WrongFileExtention_returnError()
{
//Arrange
string expectedExtention = ".csv";
var content = "Country,Quantity\nUnited Kingdom,1";
var fileName = "test.csv";
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(content);
writer.Flush();
stream.Position = 0;
//Act
IFormFile file = new FormFile(stream, 0, stream.Length, "", fileName);
//Assert
Assert.Equal(expectedExtention, Path.GetExtension(file.FileName));
}
Return true as the expected and the filename extention is name.
Hope this helps someone :).
I know this is quite an old question but here's a nice article on getting the file extension as well as a few more values:
Get File Extension in C#
I Hope That Helps :-)!

Categories

Resources