I have two folders as part of my project, in the folder "Images" is a file called "FingerprintScan.jpg". What I am trying to do is fetch this file and then save it in my other folder called "FingerPrints".
The code I am using does not throw any errors and as far as I can tell should logically work however nothing happens.
string fileName = "FingerprintScan.JPG";
string newfilename = TextBoxUsername.Text + LabelStudentID.Text + ".JPG";
string appPath1 = AppDomain.CurrentDomain.BaseDirectory + #"Images\";
string appPath2 = AppDomain.CurrentDomain.BaseDirectory + #"FingerPrints\";
string sourceFile = System.IO.Path.Combine(appPath1, fileName);
string destFile = System.IO.Path.Combine(appPath2, newfilename);
System.IO.File.Copy(sourceFile, destFile, true);
I have tried playing around and using #"~\Images\ and #"Images but had no luck.
Try this combination and make sure your directory name and source file name match with the physical directory and file
string appPath1 = AppDomain.CurrentDomain.BaseDirectory + "Images";
string appPath2 = AppDomain.CurrentDomain.BaseDirectory + "FingerPrints";
Related
What is wrong with this code? The text file is not created in the bin\Debug directory!
string path = Environment.CurrentDirectory + "/" + "Contacts.txt";
if (!File.Exists(path))
{
File.CreateText(path);
MessageBox.Show("file created successfully");
}
using (StreamWriter sw = new StreamWriter("path", true))
{
sw.WriteLine(txtCntname1.Text + "," + txtCntnum1.Text + "," + txtCntnum2.Text + "," + txtCntnum3.Text + ",");
sw.Close();
}
You have two ways of calculating EXE's directory:
1) Implicit
You can use only the file name. In this case, the file will be created in the same folder with EXE file:
File.CreateText("Contacts.txt")
2) Explicit
If you need reliable way to get the EXE's directory, then you need to use AppDomain:
string exe_dir = AppDomain.CurrentDomain.BaseDirectory;
Then, instead of manually concatenating strings to form a path, use Path.Combine method:
string file_path = Path.Combine(exe_dir, "Contact.txt");
The code (Assembly.GetExecutingAssembly().Location), offered by #TobiasTengler, won't work, for instance, for Excel/Word add-in.
Aside from that path should probably not be in quotes here:
using (StreamWriter sw = new StreamWriter("path", true))
Do not use Environment.CurrentDirectory! It does not always represent the directory you started your executable from, since it only represents your working directory.
Please use Assembly.GetExecutingAssembly().Location to get the full path of the executing assembly and then extract only the directory from it, using:
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
Alternatively you could also use AppDomain.CurrentDomain.BaseDirectory for identifying the directory your application is residing in.
Of course not using an absolute path is a possibility as well, you can create your file using a relative path, by not specifying any directory:
File.CreateText("Contacts.txt")
EDIT:
Seeing how you formed your path, please also use Path.Combine to build your paths:
var appBaseDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var contactsFilePath = Path.Combine(appBaseDir, "Contacts.txt");
I want to download a folder with its files in various mime type. My virtual path is "http://localhost/attachments/". My sub folders are "certificates/id". So when clicking on a grid i pass id to the download page. But it throws an exception like virtual path is invalid. 'http://localhost/attachments/certificates/id)'.
In below code, Request.Params[0] meant id, this points the endlevel folder which i want to make a zip folder.
Any guidance would be grateful.
using (ZipFile zip = new ZipFile())
{
string VirtualPath = ConfigurationManager.AppSettings.Get("AttachmentsShowVirtualPath");
string Path = string.Empty;
Path = "certificates" + "/";
string folderPath = VirtualPath + Path + Request.Params[0] + "/";
zip.CompressionLevel = CompressionLevel.None;
zip.AddSelectedFiles(".", Server.MapPath(folderPath), "", false);
zip.Save(Response.OutputStream);
}
First, are you using DotNetZip? I have never use it, but try changing the . to *.*
If not getting the file, try to do Directory.GetFiles to check if eveything is pointing correctly and your code has read permission over the directory https://msdn.microsoft.com/en-us/library/07wt70x2%28v=vs.110%29.aspx
Or try to use official Zip class from https://msdn.microsoft.com/en-us/library/system.io.compression.zipfile%28v=vs.110%29.aspx With this method: ZipFile.CreateFromDirectory
How should I put files paths in a StreamReader array that located in a specific dir C#?
I tried this code in different ways but I didn`t got nothing of what I need and I need one path
string fileName = "myfile.ext";
string path1 = #"mydir";
string path2 = #"\mydir";
string fullPath;
fullPath = Path.GetFullPath(path1);
Console.WriteLine("GetFullPath('{0}') returns '{1}'",
path1, fullPath);
fullPath = Path.GetFullPath(fileName);
Console.WriteLine("GetFullPath('{0}') returns '{1}'",
fileName, fullPath);
fullPath = Path.GetFullPath(path2);
Console.WriteLine("GetFullPath('{0}') returns '{1}'",
path2, fullPath);
Not clear what you're trying to do, but maybe you want a list of files in a folder to a text file? If so, you could use this code:
static void SaveFileListingToText(string folder, string outputTxtFilePath)
{
string[] files = System.IO.Directory.GetFiles(folder);
System.IO.File.WriteAllLines(outputTxtFilePath, files);
}
The end-user supplies a path, indicating where the original document is.
string DocxFileName = "C:\\WorksArshad\\3.docx";
I'd like to create a copy of the document name 3.docx as 3Version1.docx and store the copy in the same directory as the original.
How do I get the whole path without the file name and extension?
(i.e.) I need to get the "C:\\WorksArshad\\" path alone.
FileInfo file = new FileInfo(Session.FileName);
string path = file.Directory.tostring();
and then using
string fileName = Path.GetFileNameWithoutExtension(Session.FileName);
string DocxFileNamee = path + "\\" + fileName + "V1.docx";
File.Copy(Session.FileName, DocxFileNamee, true);
where in Session.FileName = "C:\WorksArshad\3.docx" and in path I'd get "C:\WorksArshad"
requirement solved .
Or
File.Copy(Session.FileName, Path.Combine(Path.GetDirectoryName(Session.FileName)
, Path.GetFileNameWithoutExtension(Session.FileName)+"V1.docx"),true);
both the above gives the solution
Error Showing while try to zip file under Azure Local Storage.
I am trying to zip .ps1 file to <filename>.ps1.zip and i am getting an error
The directory name is invalid.
code:
string scriptPath = Path.Combine(fileDirectoryPath, fileName);
string destFilePath = string.Empty;
List<string> zipFolderDetails = new List<string>();
if (!File.Exists(scriptPath))
{
throw new Exception(string.Format("ZipFile : No file exists in the source path {0}", fileName));
}
string newDirectoryPath = Path.Combine(Utilities.GetLocalDirectoryScriptPath(), Guid.NewGuid().ToString());
zipFolderDetails.Add(newDirectoryPath);
Directory.CreateDirectory(newDirectoryPath);
destFilePath = newDirectoryPath + #"\" + fileName + ".zip";
zipFolderDetails.Add(destFilePath);
//// Zipping the script file
System.IO.Compression.ZipFile.CreateFromDirectory(scriptPath, destFilePath, CompressionLevel.Fastest, false); // <--- Exception here
return zipFolderDetails;
Instead of concatenating the destFilePath, why can't you use like below.
destFilePath = Path.Combine(newDirectoryPath,fileName + ".zip");
which will give a correct path of the file you require.
we sorted out the issue. We were passing the file path as first parameter, but it should be the directory path where file needed to zip exists.
we changed the code like this and it works now
System.IO.Compression.ZipFile.CreateFromDirectory(sourceDirectory, destFilePath, CompressionLevel.Fastest, false); // <--- Exception here