I need to add a file name/file extension at the end of GetCurrentDirectory. I want to make the .txt file and write text to it
StreamWriter sw = new StreamWriter(Directory.GetCurrentDirectory, );
sw.WriteLine(a + ":Qwerty124");
sw.Close();
Is what i have so far. I want to make the file of the current directory + \example.txt
if the current directory is Sys32 it would look like this
C:\Windows\System32\example.txt
You can use the System.IO.Path.Combine method to append the file name at the end of the path. It essentially does a string append behind the scenes but it is smart enough to use the proper path separator and add it if necessary.
System.IO.Path.Combine(Directory.GetCurrentDirectory(), "example.txt");
Path.Combine
https://msdn.microsoft.com/en-us/library/fyy7a5kt(v=vs.110).aspx
Path.Combine(Directory.GetCurrentDirectory(), "example.txt")
Related
I am very new to coding and I am having some trouble in extracting a zip file to a directory.
Currently this is my code
string zipPath = "Typhoon.zip";
string extractPath = str_xp11_loc + #"\Resources\Plugins";
ZipFile.ExtractToDirectory(zipPath, extractPath);
When using code above I am getting Invalid characters in path
I have tried code that creates a txt file with the extracting location and adding the "#"\Resources\Plugins" via FileWriter and that sorta works but only extracts to the first few folders defined. When I use filewriter it creates a new line with "Resources\Plugins" When using this method it extracts to the first line for example C:\Folder1\Output.
I have tried using string result = Regex.Replace("Xp11_install.txt", #"\r\n?|\n", "");
to try and remove the the line break but that has not worked.
Is it possible to extract file using the method above?
Any insight is greatly appreciated
I'm not quite sure if this is what you are searching for, but in my case I created a ZIP file with a folder and Subfolder inside.
With the following modified code that you provided:
string zipPath = #"YourFilePath.zip";
string extractPath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
ZipFile.ExtractToDirectory(zipPath, extractPath);
I was able to extract that file, in this example to the MyPictures folder, with all the Subfolders.
If this is not what you were searching for, maybe have a look at the docs documentation:
ZipFile.ExtractToDirectory Method
I already know how to browse for an image using open file dialog. So let's say we already got the path :
string imagePath = "Desktop/Images/SampleImage.jpg";
I want to copy that file, into my application folder :
string appFolderPath = "SampleApp/Images/";
How to copy the given image to the appFolderPath programmatically?
Thank you.
You could do something like this:
var path = Path.Combine(
System.AppDomain.CurrentDomain.BaseDirectory,
"Images",
fileName);
File.Copy(imagePath, path);
where fileName is the actual name of the file only (including the extension).
UPDATE: the Path.Combine method will cleanly combine strings into a well-formed path. For example, if one of the strings does have a backslash and the other doesn't it won't matter; they are combined appropriately.
The System.AppDomain.CurrentDomain.BaseDirectory, per MSDN, does the following:
Gets the base directory that the assembly resolver uses to probe for assemblies.
That's going to be the executable path you're running in; so the path in the end (and let's assume fileName is test.txt) would be:
{path_to_exe}\Images\test.txt
string path="Source imagepath";
File.Copy(System.AppDomain.CurrentDomain.BaseDirectory+"\\Images", path);
\ System.AppDomain.CurrentDomain.BaseDirectory is to provide path of the application folder
I was trying to move a file from my Resx to my PC, but I'm keep having problems.
So I import a folder named "bad" in the Resources and I use the File.Move method to move the folder "bad" into my PC.
But the program keeps crashing because it says: Cannot create a file when its already exists.
Here the code I use:
//txtpath is the root folder. I let the user choose the root folder and save it in txtpath.text
private void btnbadname_Click(object sender, EventArgs e)
{
string source = "Resources\bad";
string destination = txtpath.Text + #"\RADS\projects\lol_air_client\releases\0.0.1.74\deploy\assets\locale\App";
File.Move(source, destination);
MessageBox.Show("脏话ID已开启, 教程请点击下面的链接");
}
The destination Directory cannot exist. In your code you are creating the Directory if it doesn't exist and then trying to move your directory, the Move Method will create the directory for you. If the Directory already exists you will need to Delete it or Move it.
See:
Cannot create a file when that file already exists when using Directory.Move
Destination supposed to have the filename as well
string destination = txtpath.Text + #"\RADS\projects\lol_air_client\releases\0.0.1.74\deploy\assets\locale\App\yourfilename.ext";
You are using File.Move to move directory, why not using Directory.Move.
The MSDN documentation will only move files from a source to a destination, while Directory.Move will move the directory itself.
If I misunderstood you, and you want to move a file;
You can check if the file exists before or not using something like:
if(File.Exists(fileName))
File.Delete(fileName);
Edit:
If you want to iterate through the directory and make sure that the file doesn't exist before moving it, you can use something like:
//Set the location of your directories
string sourceDirectory = #"";
string destDirectory = #"";
//Check if the directory exists, and if not create it
if (!Directory.Exists(destDirectory))
Directory.CreateDirectory(destDirectory);
DirectoryInfo sourceDirInfo = new DirectoryInfo(sourceDirectory);
//Iterate through directory and check the existance of each file
foreach (FileInfo sourceFileInfo in sourceDirInfo.GetFiles())
{
string fileName = sourceFileInfo.Name;
string destFile = Path.Combine(destDirectory, fileName);
if (File.Exists(destFile))
File.Delete(destFile);
//Finally move the file
File.Move(sourceFileInfo.FullName, destFile);
}
When using MoveTo, provide the full path of where you are sending the file, including the file name, eg, pic123.jpg. If you use DirectoryInfo to get an array of files and want to move any of them, append the Name property of the file to the directory path where you are sending the file.
imgFile.MoveTo("C:\myPictures\ArchiveFolder\pic123.jpg")
I am currently dealing with an issue in iPads were when you upload an image via my site the image is renamed to "image.jpg" which is an issue as if it's in the same directory it can cause issues.. so I am trying to stop it renaming the file OR to add an incremental number onto the end of it, however I can't get this to function correctly.
I have used this small snippet of code to try and fix it, however it doesn't seem to work.
if(File.Exists(filename))
{
fileName = String.Format("{0}({1}", fileName, count++);
inputFile.PostedFile.SaveAs( String.Format( "{0}\\{1}", directory, fileName ) );
}
else
{
inputFile.PostedFile.SaveAs( String.Format( "{0}\\{1}", directory, fileName ) );
}
}
File.Exists(filename) expects complete file path to see if the file exists or not.
but in the line inputFile.PostedFile.SaveAs, you are concatenating filepath to be
directory + "//" + fileName.
so does your fileName actually consists of fullpath, or just the actual filename with extension?
if fileName consists of fullPath, inputFile.PostedFile.SaveAs should not work, as it would point to invalid location, and if fileName consists of just the file name and not complete path, then, File.Exists should not work. either way your logic is flawed.
I guess, now you know, what to change.
also, instead of using a counter, simply append DateTime.Now.ToString("hhmmddttmmss") to the fileName to make it unique.
When you call:
if(File.Exists(filename))
Make sure that filename includes the full path on disk to where you save the files.
From the looks of it you may want to use Path.Combine(directory, fileName) rather than just filename on its own.
You can generate the filename as
filename = filename + DateTime.Now.Ticks.ToString();
so that it will always be the new file. I mean it will retain the old as well as new file.
I hope it will help you.. :)
try this
fileName= Path.Combine(directory,fileName );
if(File.Exists(filename))
{
fileName = fileName + DateTime.Now.Ticks;
}
inputFile.PostedFile.SaveAs( fileName );
Take a look at my code:
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
try
{
if (Directory.Exists(Path.Combine(desktopPath, "Hackers.avi")))
Directory.Delete(Path.Combine(desktopPath, "Hackers.avi"), true);
after runing the file is still exist on my desktop , why??
It is unlikely that Hackers.avi is a directory - .avi is normally used an extension for a video file (see Audio Video Interleave on Wikipedia for more information).
Try using File.Delete instead of Directory.Delete:
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
try
{
string pathToFile = Path.Combine(desktopPath, "Hackers.avi");
File.Delete(pathToFile);
// etc...
I also omitted the call to File.Exists because you don't have to check for a file's existence before deleting it. File.Delete does not throw if the file doesn't exist.
You want to delete file, sou you must use 'File.Delete'