How to find a file without specific path - c#

Im trying to send a compressed file with an email.
So the step is :
1. Compressed the file that we want to.
2. Send the email in outlook, with the attachment from the compressed file.
My problem is when the application try to search for the compressed file, it wont find because my path didnt right.
Here's the code
using (ZipFile zip = new ZipFile())
{
//zip.UseUnicodeAsNecessary = true;
zip.AddDirectory(#"Y:\"+tglskrg+"\\Result");
zip.Save(#"C:\Users\Desktop\"+tglskrg+".zip");
}
string path = Path.Combine(Directory.GetCurrentDirectory(), tglskrg + ".zip");
//Send email code(which basicly work);
my problem is , the file is saved in desktop
the actual result is , when the apps try to search for the file, the apps look in the path directory which from the code i write, the path is in the Debug folder from the application.
Anyone can help ? or maybe where did i do wrong?
Thankyou

Maybe I am not understanding your question; but, if you want to refer to the desktop folder, use Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory).
For example, you are trying to save your zip file to the desktop,
change this:
zip.Save(#"C:\Users\Desktop\"+tglskrg+".zip");
to this:
zip.Save(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), tglskrg + ".zip"));

Related

Download file from base64 in xamarin forms

I'm trying to download a file (that can be either image or PDF) that I've saved in my DB. I get the file in a string format, so I want to download this file. The problem is, I don't get any error, it just doesn't show the downloaded file on its folder.
Here is what I have so far.
string caminho = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string caminho_arquivo = Path.Combine(caminho, nome_arquivo);
var decoded = Encoding.UTF8.GetString(Convert.FromBase64String(arquivo.arquivo));
File.WriteAllText(caminho_arquivo, decoded);
The code below:
string caminho = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
would show the path like:
"/data/user/0/packagename/files/filename"
In Internal Storage, you couldn't see the files without root permission.
But you could use the code to check the file exist or not in the internal storage.
if (File.Exists(caminho_arquivo))
{
}
If you want to view it, you could use adb tool. Please refer to the way in the link about the adb tool.
How to write the username in a local txt file when login success and check on file for next login?

Getting zip files without full directory path using dotnetzip library

I am trying to download a zip of pdf files without having to go through the full directory path
So when I am downloading the files the user will have to click through the entire path to get to the pdf. I want just the files to download without the entire path.
So I did this:
public ActionResult DownloadStatements(string[] years, string[] months, string[] radio, string[] emails, string acctNum)
{
List<string> manypaths = (List<string>)TempData["temp"];
using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile())
{
zip.AddFiles(manypaths , #"\");
MemoryStream output = new MemoryStream();
zip.Save(output);
return File(output.ToArray(), "application/zip");
}
}
In the line zip.AddFiles(manypaths, #"\") I added the #"\" and that seemed to do the trick. But now at that line of code I am getting an error:
System.ArgumentException: 'An item with the same key has already been added.
None of the files are duplicates as I have checked that. I just don't understand what is going on?
I was thinking about maybe if I added a timestamp to it it might help but can't figure out the proper way to do this in dotnetzip library.

UnauthorizedAccessException when saving file, but can create a directory

I'm trying to save a file to the disk but I get UnauthorizedAccessException.
The error says I have to get the right permissions on the folder, and I've tried every possible user I can find but it doesn't work.
Tried the following users
Network
Network Services
IUSR
IUSR_[Computername]
And given the full rights without it working.
What I find really strange is that I create a directory before I try to save the file and that works perfectly, it's when trying to save a file to that new directory that I get the UnautorhizedAccessException.
The code is the following:
[HttpPost]
public ActionResult Images(HttpPostedFileBase file, string boatId)
{
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/Content/Images/" + boatId));
Directory.CreateDirectory(path);
file.SaveAs(path);
}
return View($"Filen på {boatId} har laddats upp");
}
Any ideas at what I'm missing?
Turns out what I was trying to do was saving the folder and not the file, I forgot to combine the fileName with the path.
Changed the Save part to the following:
file.SaveAs(Path.Combine(path, fileName));
Which solved the whole thing for me.

WebClient.FileDownlOad GIVIn error that the file a is accessed by another process

I am trying to download a file using webclient method DownloadFile But its giving me error that
The process cannot access the file '...\d915877c-cb7c-4eeb-97d8-41d49b75aa27.docx' because it is being used by another process.
But when I open the file by clicking it, it opening.
There are same question requesting same info but none are accepted answers.
any help will be appreciated
It may be oS that is not letting file go. Whatever it is but after searching a lot I am unable to find solution
Here is the code to create a file
Document d = new Document();
d.Save(HttpContext.Current.Server.MapPath(#"Invoice\" + iname + ".docx"));
I am using aspose word dll
and following way I am accessing it
using (var client = new System.Net.WebClient())
{
client.UseDefaultCredentials = true;
client.DownloadFile(Server.MapPath("invoice/" + Request.QueryString["id"].ToString() + ".docx"),Server.MapPath("invoice/" +Request.QueryString["id"].ToString() + ".docx"));
client.Dispose();
}
and BTW its giving same error even to files that are not creaated using code.
Give a different path where to save the download file which is different from the download source path. If you want to replace the file do it after disposing the webclient by using File.replace() method.
string downloadPath = "Your download path";
string destinationPath = "the path where the file should be saved";`//this should be different from "download path"
File.Download(downloadPath,destinationPath);

Using webclient to download images from deployed website

i deployed a website on IIS running on localhost/xxx/xxx.aspx . On my WPF side , i download a textfile using webclient from the localhost server and save it at my wpf app folder
this is how i do it :
protected void DownloadData(string strFileUrlToDownload)
{
WebClient client = new WebClient();
byte[] myDataBuffer = client.DownloadData(strFileUrlToDownload);
MemoryStream storeStream = new MemoryStream();
storeStream.SetLength(myDataBuffer.Length);
storeStream.Write(myDataBuffer, 0 , (int)storeStream.Length);
storeStream.Flush();
string currentpath = System.IO.Directory.GetCurrentDirectory() + #"\Folder";
using (FileStream file = new FileStream(currentpath, FileMode.Create, System.IO.FileAccess.ReadWrite))
{
byte[] bytes = new byte[storeStream.Length];
storeStream.Read(bytes, 0, (int)storeStream.Length);
file.Write(myDataBuffer, 0, (int)storeStream.Length);
storeStream.Close();
}
//The below Getstring method to get data in raw format and manipulate it as per requirement
string download = Encoding.ASCII.GetString(myDataBuffer);
}
This is by writing btyes and saving them . But how do i download multiple image files and save it on my WPF app folder? I have a URL like this localhost/websitename/folder/designs/ , under this URL , there is many images , how do i download all of them ? and save it on WPF app folder?
Basically i want to download the contents of the folder whereby the contents are actually images.
First, the WebClient class already has a method for this. Use something like client.DownloadFile(remoteUrl, localFilePath).
See this link:
WebClient.DownloadFile Method (String, String)
Secondly, you will need to index the files you want to download on the server somehow. You can't just get a directory listing over HTTP and then loop through it. The web server will need to be configured to enable directory listing, or you will need a page to generate a directory listing. Then you will need to download the results of that page as a string using WebClient.DownloadString and parse it. A simple solution would be an aspx page that outputs a plaintext list of files in the directory you want to download.
Finally, in the code you posted you're saving every single file you download as a file named "Folder". You need to generate a unique filename for each file you want to download. When you're looping through the files you want to download, use something like:
string localFilePath = Path.Combine("MyDownloadFolder", imageName);
where imageName is a unique filename (with file extension) for that file.

Categories