I am getting " Could not find a part of the path" error while copying a file from server to local machine. here is my code sample:
try
{
string serverfile = #"E:\installer.msi";
string localFile = Path.GetTempPath();
FileInfo fileInfo = new FileInfo(serverfile);
fileInfo.CopyTo(localFile);
return true;
}
catch (Exception ex)
{
return false;
}
Can anyone tell me what's wrong with my code.
Path.GetTempPath
is returning you folder path. you need to specify file path as well. You can do it like this
string tempPath = Path.GetTempPath();
string serverfile = #"E:\installer.msi";
string path = Path.Combine(tempPath, Path.GetFileName(serverfile));
File.Copy(serverfile, path); //you can use the overload to specify do you want to overwrite or not
You should copy file to file, not file to directory:
...
string serverfile = #"E:\installer.msi";
string localFile = Path.GetTempPath();
FileInfo fileInfo = new FileInfo(serverfile);
// Copy to localFile (which is TempPath) + installer.msi
fileInfo.CopyTo(Path.Combine(localFile, Path.GetFileName(serverfile)));
...
Related
i try to write path of folder but all of the way i write give '//' not '/'
string path = "C:\\Users\\AVITAL\\Desktop\\my-first-project\\src\\assets\\image\\";
f= Path.DirectorySeparatorChar;
string path = "C:{f}Users{f}AVITAL{f}Desktop{f}my-first-project{f}src{f}assets{f}image{f}";
string path = "#C:\Users\AVITAL\Desktop\my-first-project\src\assets\image\";
all this ways give me this exeption:
{"Could not find a part of the path 'C:\Users\AVITAL\Desktop\my-first-project\src\assets\image\83182021-07-19.jpg'."}
i want to get file from angular and save it in assets folder
public async Task<bool> addNewcustomerImage(IFormFile file)
{
try
{
string end = file.FileName;
string path = "C:\\Users\\AVITAL\\Desktop\\my-first-project\\src\\assets\\image\\";
string finalPath = path + end;
using (var stream = new FileStream(finalPath, FileMode.Create))
{
await file.CopyToAsync(stream);
stream.Close();
}
}
catch (Exception ex)
{
return false;
}
return true;
}
i think the problem in my computer because it work on my lase computer
Another way of solving your problem is using Path.Combine()
Try using the # symbol so that you don't have to escape any character in a string.
string end = file.FileName;
string path = #"C:\Users\AVITAL\Desktop\my-first-project\src\assets\image\";
string finalPath = System.IO.Path.Combine(path,end);
I have a folder full of text files. I want to zip each one individually. If there are First.txt and Second.txt in the folder C:\Temp\raw
I want to zip First.txt as First.zip and move it to C:\Temp\Done and Second.txt zipped as Second.zip and move it to C:\Temp\Done. This is the code I have
-- SourcePath = C:\Temp\raw tempPath = C:\temp\Done in app.config
public static DirectoryInfo sourcePath = new DirectoryInfo(ConfigurationManager.AppSettings["sourcePath"].ToString());
public static string tempPath = ConfigurationManager.AppSettings["tempPath"].ToString();
var files = System.IO.Directory.GetFiles(sourcePath.ToString(), "*.txt").OrderBy(f => f);
string zipfilename = "";
try
{
foreach (var fPath in files)
{
string fileNameNoPath = Path.GetFileName(fPath);
string destFile = Path.Combine(tempPath, fileNameNoPath);
System.IO.File.Move(fPath, destFile);
ZipFile.CreateFromDirectory(tempPath, tempPath, CompressionLevel.Fastest, true);
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
I am getting errors. System.UnauthorizedAccessException: Access to the path 'C:\Temp\Done' is denied
I do have access to C:\temp\folder and I am running as admin
Can someone guide me
Thanks
MR
I am writing a c# desktop app where I want users to select a file from open file dialog after which the program will copy the file to where the application is executing from: here is my code that is not working at the moment
var dlg = new Microsoft.Win32.OpenFileDialog {
Title = "Select File",
DefaultExt = ".json",
Filter = "Json File (.json)|*.json",
CheckFileExists = true
};
if (dlg.ShowDialog() == true)
{
try
{
var currentDirectory = System.Windows.Forms.Application.ExecutablePath;
var destFile = Path.Combine(currentDirectory + "/temp/", dlg.FileName);
File.Copy(dlg.FileName, destFile, true);
}
catch (Exception ex)
{
MessageBox.Show(string.Format("An error occured: " + ex.Message));
}
}
Now I am getting the error that
the file is being used by another program
. When I edit the code that is meant to initiate the copy by removing true:
File.Copy(dlg.FileName, destFile);
I get the error that the
file already exists
in the directory where it is being selected from.
It seems, that you have an incorrect path to write into.
System.Windows.Forms.Application.ExecutablePath
returns exe file itself, not directory. Try
var destFile = Path.Combine(
Path.GetDirectoryName(Application.ExecutablePath), // Exe directory
"temp", // + Temp subdirectory
Path.GetFileName(dlg.FileName)); // dlg.FileName (without directory)
If you aren't sure that temp exists, you have to create it:
Directory.CreateDirectory(Path.GetDirectoryName(destFile));
Use below Code for Copy file from one folder to another folder.
string[] filePaths = Directory.GetFiles("Your Path");
foreach (var filename in filePaths)
{
string file = filename.ToString();
//Do your job with "file"
string str = "Your Destination"+file.ToString(),Replace("Your Path");
if (!File.Exists(str))
{
File.Copy(file , str);
}
}
I am trying to move all files from rootFolderPath to destinationPath
try
{
string rootFolderPath = #"D:\Log_siteq\";
if (!Directory.Exists(Path.Combine(#"D:\Log_takaya\" + comboBox1.SelectedItem.ToString())))
{
System.IO.Directory.CreateDirectory(Path.Combine(#"D:\Log_takaya\" + comboBox1.SelectedItem.ToString()));
}
string destinationPath = Path.Combine(#"D:\Log_takaya\" + comboBox1.SelectedItem.ToString() );
string fileTypes = #"*.*";
string[] fileList = System.IO.Directory.GetFiles(rootFolderPath, fileTypes);
foreach (string file in fileList)
{
string ext = Path.GetExtension(file);
string destination = Path.Combine(destinationPath,file);
File.Move( file,destination);
MessageBox.Show(file);
MessageBox.Show(destination);
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
Apparently MessageBox.Show(file); shows me my root folder path ( as is normal) but MessageBox.Show(destination); is showing me the same thing.
What gives? It just moves my file from my root folder in the same folder.Am I not getting something?
You are combining the destinationPath with the complete file path of file:
string destination = Path.Combine(destinationPath, file);
which will just overwrite the destination with the original file path (because "C:\desitnation\C:\source\filename.txt" isn't a valid path).
Instead, you need only the file name like this:
string destination = Path.Combine(destinationPath, Path.GetFileName(file));
I am trying to copy all format file (.txt,.pdf,.doc ...) file from source folder to destination.
I have write code only for text file.
What should I do to copy all format files?
My code:
string fileName = "test.txt";
string sourcePath = #"E:\test222";
string targetPath = #"E:\TestFolder";
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
Code to copy file:
System.IO.File.Copy(sourceFile, destFile, true);
Use Directory.GetFiles and loop the paths
string sourcePath = #"E:\test222";
string targetPath = #"E:\TestFolder";
foreach (var sourceFilePath in Directory.GetFiles(sourcePath))
{
string fileName = Path.GetFileName(sourceFilePath);
string destinationFilePath = Path.Combine(targetPath, fileName);
System.IO.File.Copy(sourceFilePath, destinationFilePath , true);
}
I kinda got the impression you wanted to filter by extension. If so, this will do it. Comment out the parts I indicate below if you don't.
string sourcePath = #"E:\test222";
string targetPath = #"E:\TestFolder";
var extensions = new[] {".txt", ".pdf", ".doc" }; // not sure if you really wanted to filter by extension or not, it kinda seemed like maybe you did. if not, comment this out
var files = (from file in Directory.EnumerateFiles(sourcePath)
where extensions.Contains(Path.GetExtension(file), StringComparer.InvariantCultureIgnoreCase) // comment this out if you don't want to filter extensions
select new
{
Source = file,
Destination = Path.Combine(targetPath, Path.GetFileName(file))
});
foreach(var file in files)
{
File.Copy(file.Source, file.Destination);
}
string[] filePaths = Directory.GetFiles(#"E:\test222\", "*", SearchOption.AllDirectories);
use this, and loop through all the files to copy into destination folder