How can I find and delete a directory [closed] - c#

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.

Related

Validate if there is a folder and open your PDF file C # [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want to know the way in which I can validate if there is a folder, validate the folder by the name of the folder like Id, and if it exists, open the files it has inside, in this case they are PDF'S, I explain: I have a path of a server where I have several folders stored
enter image description here
and within those folders I have PDF files, in this case there is only one pdf file inside this folder
enter image description here
What I want to do is validate if the folder exists (by the name of the folder), and if it exists, open the files that come within that folder
What I want is to know how to show me the file (s) that come inside the folder, as long as the folder exists by name, do the whole procedure at the press of a button and start the validation and open the records.
Is this what you are looking for?
var dir = new DirectoryInfo(#"C:\Temp");
if (!dir.Exists)
return;
foreach (var file in dir.GetFiles("*.pdf", SearchOption.TopDirectoryOnly))
{
Process.Start(file.FullName);
}
You can wrap it in a method and pass the directory and not hard code it, in real life… also, you would potentially open quite a lot of PDF files like this …
when you do:
var dir = new DirectoryInfo(#"C:\Temp");
if (!dir.Exists)
return;
foreach (var file in dir.GetFiles("*.pdf", SearchOption.AllDirectories))
{
Process.Start(file.FullName);
}
You go through all your sub folders as well

I am trying to get the assembly folder path [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 5 years ago.
Improve this question
I am using the below code to get the path:
string componentName =System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
It returns the complete path:
C:\PurgeEngineIntegrated\PurgeEngine\PurgeEngine\bin\Debug
But I wanted to go to the path:
C:\PurgeEngineIntegrated\PurgeEngine\PurgeEngine\GDPR Files
The code is correct. It is returning the location from which your assembly is running. When Visual Studio compiles, that is the folder that it is put into. If GDPR Files is a folder that should be present with the run time, you can either mark it as Copy Always or Copy Only If Newer. Alternatively, you need to manually copy that folder over.
You can try this:
string componentName = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName + "\\GDPR Files";
Please note Directory.GetCurrentDirectory() this will give you the working directory path which is C:\PurgeEngineIntegrated\PurgeEngine\PurgeEngine\bin\Debug and this code Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName will give you the path before the Bin folder. If you run the program from exe file from the Debug folder, it will still work as it gives you the path of two steps up of the current location. So you have to be careful if you want to use it in different location.
You can try UriBuilder.UnescapeDataString() to get directory from assembly location
string componentName = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder oUriBuilder = new UriBuilder(componentName);
string strPath = oUriBuilder.UnescapeDataString(oUriBuilder.Path);
string strDirectory = Path.GetDirectoryName(strPath);

How to create folder in mvc4 [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I am developing one MVC4 application and hosting in IIS web server.
I want to upload and save few files in folder called UploadedFile inside F drive.
I wrote below piece of code to create folder however it does not work
if (!System.IO.Directory.Exists(Server.MapPath("~/F:/UploadedFile")))
{
System.IO.Directory.CreateDirectory(Server.MapPath("~/F:/UploadedFile"));
}
When i am hosting to IIS i will keep all the published files inside inetpub(files like dll(bin),css,js etc). But i am planning to keep pdf files uploaded by user in F drive.
Is this good practice to keep files outside c drive? can some one give some suggestions please.
There is nothing wrong with keeping your files outside of the web folder, as long as you take care of setting up security and ACL's properly. This stuff is not trivial to do and you may end with security issues if you don't configure it correctly.
In your case I think you are getting a wrong path when trying to get the data from the file.
In this line:
System.IO.File.ReadAllBytes(folderName + filename);
folderName + filename will return #"F:\UploadedFile<filename>". E.g.: #"F:\UploadedFilefile1.docx" so you'll get an error as it's an invalid path.
In order to avoid this kind of errors you should use Path.Combine.
using System.IO;
//this will return #"F:\UploadedFile\file1.docx"
var fullFileName = Path.Combine(folderName, fileName);
var bytes= System.IO.File.ReadAllBytes(fullFileName);
//do something with your file.
Hope this helps!

How to give path properly in C# [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 6 years ago.
Improve this question
I have a folder in my project named Export, I save files to that folder using this code :
document.Save(#"D:\workspace\folder1\Solution.Application.DataExporter\Export\mydocument.pdf");
But when others use this code, they complain that they don't have that path. How can I give path to my code so that it works everywhere? Thanks.
Option 1: Use the Environment.SpecialFolder to get the physical location of a special folder. See here for an overview of possible special folders.
For example, if you want to put the document in 'my documents' folder, then Environment.SpecialFolder.MyDocuments would give you the location to the my documents folder on the current machine.
Code:
var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
This way you are sure you always have the correct and existing location. If needed, you can always first create an export folder into this special folder with Directory.CreateDirectory(), if it does not exist yet.
Option 2: Of course, you can always ask for a location to the user if you don't want to use a predefined one, by using the SaveFileDialog class, for example.
Create the folder 'Export' in MyDocuments, and then save the file to that directory. As many others have pointed out. You need to save to a directory, that the executing user has access rights to.
string documentFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), #"Export");
if (!Directory.Exists(documentFolder))
{
Directory.CreateDirectory(documentFolder);
}
document.Save(Path.Combine(documentFolder, "mydocument.pdf"));

How to set path of the file to its root in OpenFileDialogBox C# [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 8 years ago.
Improve this question
Ok let me explain it again
My problem is
I want to display the image. But i want this without the opendialogfile
I tried this:
pictureBox1.Image = Image.FromFile("C:\\Users\\Abdullah\\Documents\\Visual Studio 2013\\Projects\\Maker\\Maker\\add.png");// it works
But i dont want to do this because it will cause errors at deployment time. What i want to do is:
pictureBox1.Image= Image.FromFile("add.png");// because this picture is already in the project folder
In this case it show me error that file not found
Now Hope so I explained it :)
Assuming that you are hard coding the path to your image and the image really exists in that path, then you should remember to escape the backslash when using a constant string like that one.
Try with
pictureBox1.ImageLocation = #"C:\Users\Abdullah\Documents\Visual Studio 2013
\Projects\Maker\Maker\Resources\add.png";
or
pictureBox1.ImageLocation = "C:\\Users\\Abdullah\\Documents\\Visual Studio 2013
\\Projects\\Maker\\Maker\\Resources\\add.png";
(Warning strings splitted in two lines for readability. It should be on one single line)
See How do I write a backslash?
EDIT:
Based on your comment below, then it seems that the Image folder always exists in your project (also when it will be deployed to a customer machine) then you could write something like this
pictureBox1.ImageLocation = Path.Combine(Application.StartupPath, "Images", "add.png");
or
string imageFile = Path.Combine(Application.StartupPath, "Images", "add.png");
pictureBox1.Image= Image.FromFile(imageFile);
But looking back to your example: Is it Images or Resources?

Categories