I want to save image into a folder and the path of the folder to the database.
I have done this with File.Copy(filepath) command but it is giving me error when a file with the same name already exists there.
Second thing in this command is that I have to provide a filename in it from which it is copying the file. If I am modifying a record and not the image then it is giving error that file source cannot be empty.
I have also tried Picture1.image.save(filename) but I have not found any command to overwrite the existing file.
Please help me by providing a simplest way to do all this.
There's an overload to the File.Copy() method that accepts a bool which will determine whether to overwrite any existing files with the same name.
http://msdn.microsoft.com/en-us/library/9706cfs5.aspx
File.Copy(sourceFileName, destFileName, true) Will force an overwrite of existing file.
Refer MSDN File.Copy
if(File.Exists(destinationFileName))
{
File.Delete(destinationFileName);
}
File.Copy(sourceFileName, destinationFileName);
sourceFileName shoudl be the full path of the source file(including the file name).
destinationFileName should be the fullpath (including the filename) where you want to save the file.
you have to first check whether file exists or not?
using FileInfo,
FileInfo file = new FileInfo(location);
if(file.Exists())
{
File.Delete(location);
File.Copy(srcLocation, location);
}
In this way you can avoid the error.
Related
File.Copy(#"my program\\subfolder\\what i want to copy.txt", "C:\\Targetlocation");
How can i copy a text file from one folder to another using relative path.
To execute the File.Copy the source and destination will be a valid file path. in your case the destination is a folder not File. in this case you may get some exception like
Could not find a part of the path 'F:\New folder'
While executing the application, the current directory will be the bin folder. you need to specify the relative path from there. Let my program/subfolder be the folders in your solution, so the code for this will be like this:
string sourcePath = "../../my program/subfolder/what i want to copy.txt";
string destinationPath = #"C:\Targetlocation\copyFile.txt"
File.Copy(sourcePath, destinationPath );
Where ../ will help you to move one step back from the current directory. One more thing you have to care is the third optional parameter in the File.Copy method. By passing true for this parameter will help you to overwrite the contents of the existing file.Also make sure that the folder C:\Targetlocation is existing, as this will not create the folder for you.
File.Copy(#"subfolder\\what i want to copy.txt", "C:\\Targetlocation\\TargetFilePath.txt");
The sourceFileName and destFileName parameters can specify relative or
absolute path information. Relative path information is interpreted as
relative to the current working directory. This method does not
support wildcard characters in the parameters.
File.Copy on MSDN
Make sure your target directory exists. You can use Directory.CreateDirectory
Directory.CreateDirectory("C:\\Targetlocation");
With Directory.CreateDirectory(), you don't have to check if the directory exists. From documentation:
Any and all directories specified in path are created, unless they
already exist or unless some part of path is invalid. The path
parameter specifies a directory path, not a file path. If the
directory already exists, this method does nothing.
// Remove path from the file name.
string fName = f.Substring(sourceDir.Length + 1);
try
{
// Will not overwrite if the destination file already exists.
File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName));
}
You can provide the relative path from your current working directory which can be checked via Environment.CurrentDirectoy.
For example if your current working directory is D:\App, your source file location is D:\App\Res\Source.txt and your target location is D:\App\Res\Test\target.txt then your code snippet will be -
File.Copy(Res\\Source.txt, Res\\Test\\target.txt);
How can I check if a file exists or not in the directory the executable is?
I know how I could code it like this.
string path = Application.StartupPath + "config.cfg"
if(!FileExists(path))
{
//create the file
}
But the problem I am facing is that, the file is created every single time, even when the file exists, overwriting the data of the cfg file with the default ones.
You are not creating the possible file path properly. Use Path.Combine like:
string path = Path.Combine(Application.StartupPath, "config.cfg");
You are getting a path without terminating \ from Application.StartupPath, later you are concatenating the file name to it, This will create an invalid path, and since that doesn't exist, you check fails.
Just to show, the actual reason for getting the error, you can fix your code like:
string path = Application.StartupPath +"\\"+ "config.cfg";
But, do not use the above code, instead use Path.Combine to join multiple path elements.
I've got a file named test_file which is a file without extension, the path is 'C:\share\'. And I want to copy it to a new folder so the code is:
File.copy(#"C:\share\test_file", #"C:\share\newFolder\test_file", true);
And it will throw an exception:
DirectoryNotFoundException: Could not find a part of the path C:\share\newFolder\test_file
Does anyone know how to solve this?
Do following
//get name of directory where you are copying the file to
var dir = Path.GetDirectoryName(#"C:\share\newFolder\test_file");
//create directory (following command will create all the missing folders in path)
Directory.CreateDirectory(dir);
File.Copy(#"C:\share\test_file", #"C:\share\newFolder\test_file", true);
Does newFolder exist? I'm guessing it doesn't. You need to create that folder, for example, using Directory.CreateDirectory it will create all necessary folders among given path. So it would be like:
Directory.CreateDirectory("C:\\share\newFolder\\");
and then
File.Copy(#"C:\share\test_file", #"C:\share\newFolder\test_file", true);
I want to copy a file to a directory. I thought it would be a simple enough process.
This is the code im using:
string strSrcPath = "C:\Users\Documents\Development\source\11.0.25.10\",
strDstPath = "C:\Users\Documents\Development\testing\11.0.25.10\",
strFile = "BuildLog.txt"
File.Copy(Path.Combine(sourcePath, sourceFile), strDstPath);
The problem here is that when i'm doing the File.Copy it wants to copy one file to another, but I dont want to do that since the file does not exist in the destination path. Therefore I get thrown an error which states something along the lines of 'Cannot copy, strDstPath is a destination not a file"
Was there something I could use instead of File.Copy to copy a file that doesnt exist in the destinaion from the source to destination?
The problem is that the parameters are the source filename and the destination filename. You are passing a destination directory and the program is confused because you can't make the file into a directory.
Use instead:
File.Copy(Path.Combine(strSrcPath , strFile ), Path.Combine(strDstPath, strFile);
You seem to be passing some wrong parameter to the Path.Combine (the second one). It should be strFile instead of sourceFile which is quite unclear where is it coming from.
And you also need to provide a filename for the destination folder:
File.Copy(Path.Combine(sourcePath, strFile), Path.Combine(strDstPath, strFile));
You also need to escape the \ characters in your string because your code will probably not compile. This could be done by either using \\ or by using the # character at the beginning of your string.
string strSrcPath = #"C:\Users\Documents\Development\source\11.0.25.10\",
strDstPath = #"C:\Users\Documents\Development\testing\11.0.25.10\",
strFile = "BuildLog.txt"
File.Copy(Path.Combine(sourcePath, strFile), Path.Combine(strDstPath, strFile));
Also make sure that the destination folder you specified exists. If it doesn't exist you need to create it first (using the Directory.CreateDirectory method).
You have to specify a filename for your destination
so
File.Copy("XMLFile1.xml", #"c:\temp");
will fail where
File.Copy("XMLFile1.xml", #"c:\temp\XMLFile1.xml");
will not
In a C# program, I am creating files. I want to delete one file using this command:-
File.Delete(killFile);
The killFile has a value = "C:\Documents and Settings\MehdiAnis\My Documents\outfile_0020.csv"
The killFile is an existing file.
After I run Delete command, file is still in the Directory. Right after delete I added FileInfo code to check if the file exists,
FileInfo fi = new FileInfo(killFile);
Now, fi.Exists shows false
I am not sure what's wrong, can it be permission issue? I just wrote the file in my own folder, why can't I delete it? Once the file is created I am not opening it or doing anything with it, so it should not be locked.
What could be wrong and where else should I be looking?
Per the screenshot you posted at http://i548.photobucket.com/albums/ii341/MehdiAnis/cprob.jpg
In your screen shot, the explorer window is showing a file with name eding in "_0020.csv" . You are passing in a filename ending with "_20.csv", according to the debugger window. You are calling File.Delete with the name of a file that doesn't actually exist, and so no file is deleted.
You will want to format your "killFile" variable with 0 padding. I assume you are adding some counter to it like killfile = killFile + i.ToString(). Try killfile = killFile + i.ToString("0000")
According to MSDN, "If the file to be deleted does not exist, no exception is thrown."
You may want to check for existence of the file to be deleted using File.Exists before trying to delete it. I think your problem is the file you are expecting to delete isn't the file that you see in the folder.