How to delete a file with no extension? [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm developing a programm and the programm schould delete a cache. The only problem is that all the files in the cache have no extensions and always when I try to delete those files I get an exception: The access to the path 'path' was denied. So I don't know why this error occurs because I'm trying to delete a file and not a directory. I think it doesn't recognises the file as a file because of the missing extension.I've etried this:
DirectoryInfo cache = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + #"\Cache");
Console.WriteLine("Deleting Cache...");
foreach (var fi in cache.GetFiles())
{
File.Delete(cache + #"\" + fi.Name);
Console.WriteLine(cache + #"\" + fi.Name);
}
Console.ReadKey();
What else can I try?

It seems your file was still open, so you can't delete it. However, my original answer still applies:
Since you already have a DirectoryInfo, you dont need to concatenate the strings to get the file-name, just use FileInfo.Delete:
foreach (var fi in cache.GetFiles())
{
fi.Delete();
}
You should avoid concatenating strings to build a path anyway, use Path.Combine:
DirectoryInfo cache = new DirectoryInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Cache"));

Related

How can I find and delete a directory [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am trying to make an application that deletes a folder and I cannot seem to find a way to find what user they are on and implement it into the directory deleting. I am coding in c# on Visual Studio
edit:
os: windows 10
basically I want to find the username of the computer and set it as a variable than go and delete a certain folder inside of C:/users/(user)
I have a way to get the user which is Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); But I want to set that as a variable and then use that variable to replace
DirectoryInfo di = new DirectoryInfo(#"C:\Users\Desktop
Path\yes");
foreach(FileInfo file in di.GetFiles())
{
file.Delete();
}
I don't know why would you need to get the user, but ok. You already have the solution using Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); that will get you something like "C:\Users\randomUserName". You can store that in a variable. Then append whatever folder path you need using Path.Combine, you don't really need to use that function but it's best practice and it just saves you so much trouble. So the final product will be something like this.
var userPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
var path = Path.Combine(userPath, "you path here");
var dirInfo = new DirectoryInfo(path);
//Rest of your code here.

File.Exists returning True when should be False [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm working on a program which requires the lookup of files in user defined directories. If the files don't exist, it must continue to use it's own resources. The problem is, even though some files don't exist, the function File.Exists returns true for them.
Here is an image of the error:
Here is the code that looks for the file and produces the error:
string[] vmtLines = null;
if (File.Exists(vmtFile))
{
try { vmtLines = File.ReadAllLines(vmtFile); }
catch (Exception e) { Debug.Log(vmtFile + " Exists: " + File.Exists(vmtFile) + ", " + e.Message); }
}
It turns out the problem is not in C#, Unity, or my programming. It seems there's a problem with files that are called con. I was testing out in the cmd to see if I could open the file con.vmt with notepad, here's what happened:
Then I tried to see if the same thing happens with other files in the same directory:
It opened just fine. Then I tried to see if it were just 3 letter file names which was the problem by opening a file with the name com:
It didn't have some weird problem, then I tried to see if it was something with the extension vmt and the file name con:
It had the same problem. I tried the same thing in the parent directory, and the same problem occurred with the file name con. So I assume this happens anywhere.
Thank you #Programmer, #CharlesMager, #AlexK, #Quantic, #ScottChamberlain, and #LarsTech for trying to help. This was one strange problem

bitmap.save subfolder with string value [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
My code
string userid;
string userimageid;
bitmap.save(#"C:\temper\temp\"+userid+"\"+userimageid+".jpg");
get build error! why canĀ“t I create subfolder with string value name and insert image? still cant insert to that crated folder as name userid.
Yes, you can. But you must create first the folder and then save the bitmap:
string folderName = #"C:\temper\temp\"+userid;
if (!Directory.Exists(folderName) {
Directory.CreateDirectory(folderName);
}
bitmap.save(folderName + "\\userimage.jpg"); //your original concatenation had an error
Solved!
string folderName = #"C:\temper\temp\"+userid;
if (!Directory.Exists(folderName) {
Directory.CreateDirectory(folderName);
}
bitmap.save(folderName + "\\" +userimage+ ".jpg"); `Create first folder and save Thanks Mnieto`

C# Directory.GetFiles adds extra file extension to returned path [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
At the moment I'm currently trying to get all files from a specified directory. The files I'm looking for end with the file type .ship, however, my code returns "path\NameOfFiles.ship.ship" and I can't for the life of me decide where the extra file extension is added.
Here's the function I use
public static List<SaveEntry> GetAllSavesFromProfile(string ProfileName)
{
if (IsProfileMissing(ProfileName))
{
Debug.LogError("Couldn't find save from profile. Check if it exists before calling this function");
return new List<SaveEntry>();
}
List<SaveEntry> ListToReturn = new List<SaveEntry>();
List<string> SaveEntries = Directory.GetFiles(Application.persistentDataPath + "/Profile_" + ProfileName, "*", SearchOption.TopDirectoryOnly).Where(s => s.EndsWith(".ship", StringComparison.CurrentCultureIgnoreCase)).ToList<string>();
foreach (string entry in SaveEntries)
{
ListToReturn.Add(LoadShip(ProfileName, entry));
}
return ListToReturn;
}
I'm pretty I'm doing something wrong with the Where function, but I can't decide exactly where.
I suspect your filenames contains an extra .ship. You can verify that if you change your settings to show file extensions.
Also, you can use a search pattern instead of using Where
Directory.GetFiles(Application.persistentDataPath + "/Profile_" + ProfileName, "*.ship", SearchOption.TopDirectoryOnly)

File Not Found when opening to read a file [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I enter a path name and a file name into my program, that works enough, and eventually the path might be something like
path = #"C:\ ...\";
Where the ... is just the rest of the path to the directory where the below file is located.
file = "something.txt";
But I get a file not found when I execute this:
System.IO.StreamReader reader = new System.IO.StreamReader(path+file);
I'm confused as to what I'm doing wrong. Am I just not using this correctly?
I'm an idiot sorry, I found out what I was doing wrong.
You can try to use Path.Combine, but if you assign your full path directly to a single variable it would better,I don't understand why are you doing this by the way.
StreamReader reader = new StreamReader(Path.Combine(path,file));
Note: Delete last backslash from the path variable
Create a FileInfo object to make your life a bit easier.
FileInfo file = FileInfo(Path.Combine(path,file));
if(!file.Exists)
throw new FileNotFoundException("File Not Found or Inaccessable"); //or handle approprately
using(StreamReader reader = file.OpenText())
{
//do reading stuff here
}

Categories