the given path's format is not supported. c# - c#

OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Select Student Picture";
ofd.InitialDirectory = #"C:\Picture";
ofd.Filter = "All Files|*.*|JPEGs|*.jpg";
//ofd.Multiselect = false;
if (ofd.ShowDialog()==DialogResult.OK)
{
if (ofd.CheckFileExists)
{
pbStudent.ImageLocation = ofd.FileName;
string path = ofd.SafeFileName;
System.IO.File.Copy(ofd.FileName, "/Resources/SImages/" + lblRNo + ".jpg");
}
}
Can some one help me resolve issue with this error message:
the given path's format is not supported.

Here you use the correct way of formatting the path in Windows with a Backslash
ofd.InitialDirectory = #"C:\Picture";
And in the next line you divert from it
System.IO.File.Copy(ofd.FileName, "/Resources/SImages/" + lblRNo.Text + ".jpg");
just keep the way you did it in the beginning:
System.IO.File.Copy(ofd.FileName, #"\Resources\SImages\" + lblRNo.Text + ".jpg");
One way of avoiding such irritations is to use System.IO.Path.Combine()
string path = System.IO.Path.Combine("Resources", "SImages");
EDIT: due to the extraordinary observation by Steve I changed lblRNo to lblRNo.Text

Your problem is the lblRNo variable used to express the name of the destination file. It seems that this is a label of your program and not a string containing your file name. To get the correct value you should use the property Text of the label.
System.IO.File.Copy(ofd.FileName, #"/Resources/SImages/" + lblRNo.Text + ".jpg");
Of course, having specified a relative path then you should be sure that this path exists relative to the root drive where your code is executing.
For example, if your code runs on the C: drive then a path
C:\Resources\SImages
should exist otherwise you get the execption about a part of your path not being found.
As other have said, the proper path separator in Windows is the backslash but you could also use the forward slash without any problem

Try this instead:
System.IO.File.Copy(ofd.FileName, #"\Resources\SImages\" + lblRNo + ".jpg");
the / is for the internet, the \ is for inside your computer :)

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
}

How to correctly format a c# 7-zip string with a password?

So I am coding an encryption application in C#, which calls on 7zip to encrypt a folder into a 7zip archive with a previous entered password. The trouble is, for some reason it's seeing the file I am trying to compress into the 7zip archive as a 7zip file itself, when it actually is just a normal folder so not sure why that's happening. Here is the code:
string sourceName = #"c:\putfilesforencryptionhere";
string targetName = #"c:\encryptedfileshere.7z";
string password = Form2.verify;
// Initialize process information.
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = "C:\\Program Files\\7-Zip\\7zG.exe";
// Use 7-zip
// specify a=archive and -tgzip=gzip
// and then target file in quotes followed by source file in quotes
p.Arguments = "a \"" + targetName + " -p" + password + " " + sourceName;
And when running the program, 7zip returns this error:
The filename, directory name, or volume label syntax is incorrect. cannot open file c:\encryptedfileshere.7z -p09/28/2020 11:17:29 AM c:\putfilesforencryptionhere.7z.
String password = Form2.verify as it is a password that is entered in a previous form. I wonder why 7-zip would see c:\putfilesforencryptionhere as a 7zip file when it is a normal folder?
Thanks very much.
When setting the value of p.Arguments, there is an escaped quote \" before targetName but not after. Therefore, the entirety of the following string is being interpreted as the archive name (as seen in the error message).
Try
p.Arguments = "a \"" + targetName + "\" -p" + password + " " + sourceName;
Or use ArgumentList to avoid escaping issues.
p.ArgumentList.Add("a");
p.ArgumentList.Add(targetName);
p.ArgumentList.Add("-p" + password);
p.ArgumentList.Add(sourceName);

How to move a file from a default directory to another?

It sounds simple but it is not. I am trying to move a file that i made it like this:
string newFileName = string.Format("{0}-{1}-{2}-t{3:00}-{4:00}.txt", 2013, 10, 5, 05, 06);
It is going to look like: 2013-10-5-05-06.txt, from the default directory (..\bin\debug\2013-10-5-05-06.txt) to another directory (c:\Users\Public\Folder). I want to keep the name of the file so that other files having almost the same name (small difference between) being moved to the same folder. I tried several methods (Path.Combine(), string.Concat()..) without success.
Just use this snippet
string CurrentFileNameAndPath; //the path the file you want to move
string newPath; //only the new the folderPath
System.IO.FileInfo FileYouWantToMove = new System.IO.FileInfo(CurrentFileNameAndPath);
string NewFileNameAndPath = newPath + "\\" + FileYouWantToMove.Name; //remember that using fullname will get the folder and filename
FileYouWantToMove.MoveTo(NewFileNameAndPath);
So lets use this as an example i have this file C:/Dir1/file1.txt and I want to change its directory to C:/Dir2/ right? then it will be like this
string CurrentFileNameAndPath = #"C:/Dir1/file1.txt";
string newPath = #"C:/Dir2/";
System.IO.FileInfo FileYouWantToMove = new System.IO.FileInfo(CurrentFileNameAndPath);
string NewFileNameAndPath = newPath + "\\" + FileYouWantToMove.Name;
FileYouWantToMove.MoveTo(NewFileNameAndPath);
the result will the that file in C:/Dir1/file1.txt will be now in C:/Dir2/file1.txt it have been moved and maintened the same file name and extension
Something like this is actually pretty trivial
var srcFile = "..\bin\debug\2013-10-5-05-06.txt";
var destFolder = Path.GetDirectoryName(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory));
var destFile = Path.Combine(destFolder, Path.GetFileName(srcFile));
File.Move(srcFile, destFile);
Just keep in mind that Move can throw various exceptions e.g. IOException / UnauthorizedAccessException etc. so it would be wise to handle these where appropriate.

File uploading into server

I am making a website having image upload module.In my localhost server its working perfectly. That means i can upload images and save it. But when i host my solution i am getting a error. That is, Access to the path is denied.
This is the code i have used...
string fileName = FileUpload1.FileName.ToString();
string uploadFolderPath = "~/up_foto/";
string filePath = HttpContext.Current.Server.MapPath(uploadFolderPath);
FileUpload1.SaveAs(filePath + "\\" + fileName);`
What the wrong in this.. Please help me....
Thanks in advance....
I am afraid there's nothing wrong with your code, if it runs locally. Instead, you have to make sure if on the host environment the user "IUSER", or "IIS_IUSER", or the like, has access (Read/Write) to the upload folder.
Since you are getting "Access to the path is denied", Did you check the folder you are trying to upload is having write access
you can use Path.combine or server.mappath (dont forget to add System.IO in the namespaces)
string fileName = FileUpload1.FileName.ToString();
string uploadFolderPath = "~/Uploads/Images/";
string filePath1 = Server.MapPath(uploadFolderPath + fileName);
or
string fileName = FileUpload1.FileName.ToString();
string uploadFolderPath = "~/Uploads/Images/";
string filePath = Server.MapPath(uploadFolderPath);
string filePath1= Path.Combine(filepath1 + fileName);

How save uploaded file? c# mvc

I want upload an image file to project's folder but I have an error in my catch:
Could not find a part of the path 'C:\project\uploads\logotipos\11111\'.
What am I do wrong? I want save that image uploaded by my client in that folder... that folder exists... ah if I put a breakpoint for folder_exists3 that shows me a true value!
My code is:
try
{
var fileName = dados.cod_cliente;
bool folder_exists = Directory.Exists(Server.MapPath("~/uploads"));
if(!folder_exists)
Directory.CreateDirectory(Server.MapPath("~/uploads"));
bool folder_exists2 = Directory.Exists(Server.MapPath("~/uploads/logo"));
if(!folder_exists2)
Directory.CreateDirectory(Server.MapPath("~/uploads/logo"));
bool folder_exists3 = Directory.Exists(Server.MapPath("~/uploads/logo/" + fileName));
if(!folder_exists3)
Directory.CreateDirectory(Server.MapPath("~/uploads/logo/"+fileName));
file.SaveAs(Server.MapPath("~/uploads/logo/" + fileName+"/"));
}
catch(Exception e)
{
}
Someone knows what I'm do wrong?
Thank you :)
Try this:
string targetFolder = HttpContext.Current.Server.MapPath("~/uploads/logo");
string targetPath = Path.Combine(targetFolder, yourFileName);
file.SaveAs(targetPath);
Your error is the following:
bool folder_exists3 = Directory.Exists(Server.MapPath("~/uploads/logo/" + fileName));
if(!folder_exists3)
Directory.CreateDirectory(Server.MapPath("~/uploads/logo/"+fileName));
You check if a directory exists, but you should check if the file exists:
File.Exists(....);
You need filename
file.SaveAs(Server.MapPath("~/uploads/logo/" + fileName+"/" + your_image_fillename));
Remove the last part of the path to save you have an extra "/"
It should be
file.SaveAs(Server.MapPath("~/uploads/logo/" + fileName);
Also you do not have a file extension set.

Categories

Resources