I am developing a software for downloading a website in C# but I have some trouble in copying the folder from server to the local directory. I am implementing following code for this purpose;
public static void CopyFilesRecursively(DirectoryInfo source, DirectoryInfo target)
{
try
{
foreach (DirectoryInfo dir in source.GetDirectories())
CopyFilesRecursively(dir, target.CreateSubdirectory(dir.Name));
foreach (FileInfo file in source.GetFiles())
file.CopyTo(Path.Combine(target.FullName, file.Name));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Form2", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
And the function call is
private void button4_Click(object sender, EventArgs e)
{
try
{
CopyFilesRecursively(new DirectoryInfo(#"https:facebook.com"), new DirectoryInfo(#"G:\Projects\"));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Form2", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
The message box shows that "the given path format is not supported."
As we are aware, all the web sites hosted on internet uses virtual path's (which are more readable and provide more security) for their files and folders. Actual files and folders are on a server behind those virtual path. So to copy a file or a folder from a remote server we need the actual path of the resource.
I provide following code snippet for downloading a file from a server which deployed by my self (I know the directory structure of it of course)
string filename = "MyPage.xml";
string filesource = Server.MapPath("~/MyFiles/") + filename; // server file "MyPage.xml" available in server directory "files"
System.IO.FileInfo fi = new System.IO.FileInfo(filesource);
string filedest = System.IO.Path.GetTempPath()+"MyFile.xml";
fi.CopyTo(filedest);
These are some other SO posts you can look for;
How to download a file from a URL in C#?
Copy image file from web url to local folder?
how to copy contents of a website using a .net desktop application
how to copy all text from a certain webpage and save it to notepad C#
How do you archive an entire website for offline viewing?
Related
I am using below code in service file for auto downloading. It works in my system and our network drive, but when i installed it client pc not working but it download in all folders except server shared location.
try
{
filename = #"N:\Shared\tna related\tna (for processing)\TNA DATA\Timesuite\LOC" + filename + ".csv";
StreamWriter file = new StreamWriter(filename);
file.WriteLine(sb.ToString());
file.Close();
TraceImplogs.TraceLTService("Download Completed");
}
catch (Exception es)
{
TraceImplogs.TraceLTService("error 2"+es.Message);
}
Error Shows in client pc:
Could not find a part of the path 'N:\Shared\tna related\tna (for processing)\TNA DATA\DXB2018-05-12.csv'.
I am trying to copy files from android mobile which is connected to PC through USB port.
This code is working for copy files from one folder to anther in PC. But when i try to copy from mobile it show "Path Not Found" MessageBox.
Please help me for this problem.
if (System.IO.Directory.Exists(#"This PC\M Hassan\Phone\Lec\Android\Mid"))
{
string[] files = System.IO.Directory.GetFiles(#"This PC\M Hassan\Phone\Lec\Android\Mid");
// Copy the files and overwrite destination files if they already exist.
foreach (string s in files)
{
// Use static Path methods to extract only the file name from the path.
fileName = System.IO.Path.GetFileName(s);
destFile = System.IO.Path.Combine(destFolder, fileName);
System.IO.File.Copy(s, destFile, true);
}
MessageBox.Show("Successfully Configured.", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Path Not Found.", "Field Required", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
I'm working on a project that will be zipping directories using the DotNetZip reference. When I use the code:
using(ZipFile zip = new ZipFile())
{
try
{
zip.AddDirectoryByName(label39.Text);
zip.Save(#"C:\SecZipped.zip");
}
catch(Exception ex)
{
MessageBox.Show("ERROR:\n\n" + ex.Message);
return;
}
MessageBox.Show("Finished.");
}
I get the error: Access to the path C:\DotNetZip-04wnaxtt.tmp is denied.
However, when I change #"C:\SecZipped.zip" in the
zip.Save(#"C:\SecZipped.zip");
to "SecZipped.zip" I get no error but the file is stored where the DotNetZip reference is located.
So can I specify where the zip file is saved?
Thanks for the help.
I fetch a path from the Web.config file:
<appSettings>
<add key="Server" value="http://admin.xxxxxxx.com/1upload/*All File And Folder Copy this directory *"/>
<add key="Local" value="C:\\Users\\IND_COM\\Desktop\\xxxx\\1upload\\paste The Copyed File"/>
</appSettings>
In the button click event I pass the source and destination path:
protected void btnUpdate_Click(object sender, EventArgs e)
{
string Source_path = ConfigurationManager.AppSettings["server"].ToString();
string Detination_Path = "#" + ConfigurationManager.AppSettings["Local"].ToString();
DirectoryCopy(Source_path, Detination_Path, true);
}
private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
DirectoryInfo dir = new DirectoryInfo(sourceDirName);//Exception Through In this Section..
if (!dir.Exists)
{
throw new DirectoryNotFoundException("Source directory does not exist or could not be found: "+ sourceDirName);
}
DirectoryInfo[] dirs = dir.GetDirectories();
if (!Directory.Exists(destDirName))
{
Directory.CreateDirectory(destDirName);
}
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
string temppath = Path.Combine(destDirName, file.Name);
file.CopyTo(temppath, false);
}
if (copySubDirs)
{
foreach (DirectoryInfo subdir in dirs)
{
string temppath = Path.Combine(destDirName, subdir.Name);
DirectoryCopy(subdir.FullName, temppath, copySubDirs);
}
}
}
On my hosting server, I have a folder and all images and XML saved in different folders. For example, I have 1 directory named 1Upload. Inside this directory I have 3 sub-directories and each has different files. I try to copy all 3 directories and files from the Server. This piece of code gives an exception
URI is not supported
HTTP is not a file system.
What you're looking to do is not to perform a file system copy operation, but rather to download the content from the web and save it to your file system. For this, you want something like the WebClient.DownloadFile() method.
For example:
var client = new WebClient();
client.DownloadFile(sourceURL, destinationFile);
Each file would be a separate request. You're currently trying to list directory contents of a website, which clearly won't work (because HTTP isn't a file system). However you build your list of files, once you have that list you can loop over it and call DownloadFile(source, destination) for each file.
If you need the website to provide a list of files, you have to add that functionality to the website (if it's not already there, we don't know). If you need to download all of the files in one operation, then the website would need to provide them in a single file (such as a .zip file).
I have written two methods such as FileUpLoad() and FileDownLoad() to Upload and Download a single file in my local system.
void FileUpLoad()
{
string hfBrowsePath = fuplGridDocs.PostedFile.FileName; //fuplGridDocs is a fileupload control
if (hfBrowsePath != string.Empty)
{
string destfile = string.Empty;
string FilePath = Path.Combine(#"E:\Documents\");
FileInfo FP = new FileInfo(hfBrowsePath);
hfFileNameAutoGen.Value = PONumber + FP.Extension;
destfile = FilePath + hfFileNameAutoGen.Value; //hfFileNameAutoGen is a hidden field
fuplGridDocs.PostedFile.SaveAs(destfile);
}
}
void FileDownLoad(LinkButton lnkFileName)
{
string filename = lnkFileName.Text;
string FilePath = Path.Combine(#"E:\Documents", filename);
fuplGridDocs.SaveAs(FilePath);
FileInfo fileToDownLoad = new FileInfo(FilePath);
if (fileToDownLoad.Exists)
{
Process.Start(fileToDownLoad.FullName);
}
else
{
lblMessage.Text = "File Not Saved!";
return;
}
}
While running the application before hosting it in IIS, I can upload a file to the desired location and can also retrieve a file from the saved location. But after publishing it in the localhost, I can only Upload a file. I could not download the saved file. There is no exception too. The Uploaded file is saved in the desired location. I don't know why it is not retrieving the file? Why I cant download the file in IIS? I have searched a lot in the internet, but couldn't find the solution. How to solve this? I am using Windows XP and IIS 5.1 version.
How do you expect your Web Application to do a Process.Start when you deploy this site to a server, your just going to be opening pictures on the server, not on the client PC.
I think this will answer your question: http://www.codeproject.com/Articles/74654/File-Download-in-ASP-NET-and-Tracking-the-Status-o
Also the download file is missing a slash after E:\Documents
another option is to add your wildcard to IIS MIME types