How to create folder in mvc4 [closed] - c#

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!

Related

Programmatically identify PHP and ASP include dependencies [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 6 years ago.
Improve this question
I am trying to automate the cleanup of legacy/heritage code and I can identify current resources in use from IIS logs but server side pages like .ASP and .PHP have include chains which I would like to be able to identify via code (C#)
In order to narrow the question down as it was too broad ...
OS - Windows 10
Web Server - IIS
Preferred language - C#
Example - any IIS served website
I have code that reads IIS log files where I have identified all static served resources including the initial .ASP or .PHP pages.
I required some accompanying C# code to also detail any .ASP or .PHP included files that were processed on the server and therefore do not appear in the IIS logs.
As it happens I have found the solution and provided an answer below.
I hope this is enough detail to take this off 'On Hold'
Many thanks to #Progrock for pointing me to checking last accessed time for files in target folder. This is what I had to do.
Ensure capturing last accessed time is being set by Windows (see Directory.GetFiles keeping the last access time)
Process.Start("fsutil", "behavior set disablelastaccess 0").WaitForExit();
Also need to restart IIS to stop any cached in memory files
Process.Start("IISRESET").WaitForExit();
A refresh on FileInfo needs to be performed to ensure you get the correct last accessed time (see How can System.IO.FileSystemInfo.Refresh be used).
This resulted in the following code ...
var start = DateTime.UtcNow;
Process.Start("fsutil", "behavior set disablelastaccess 0").WaitForExit();
Process.Start("IISRESET").WaitForExit();
//do work that will access folder. i.e. access IIS web pages
var files = Directory.GetFiles(iisPath, "*.*", SearchOption.AllDirectories).ToList();
files = files.Where(x =>
{
var fileInfo = new FileInfo(x);
fileInfo.Refresh();
return fileInfo.LastAccessTimeUtc >= start;
}).ToList();
I can use this list of files to identify all .ASP and .PHP and any other files that were accessed via the IIS server on page requests.

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 get remote folder names, select them and delete them in 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 6 years ago.
Improve this question
I am brand-new in C# and I wanna do that in C#.
Can you show me the way :)
Enter a remote machine hostname
get list folder names in C directory from the remote machine
select folder names from the list
delete the selected folders
show a message about the process (deleted or not)
Is that too hard? Thank you for your help in advance and sory for my bad English :(
Remote and local file system access in C# (.NET) works the same way. Try for example the following.
var directory = new System.IO.DirectoryInfo("\\server\path\remote\C");
var files = directory.GetFiles();
foreach(var f in files) f.Delete();
For remote drives, for example drive C, the path will be like: \server\c$\folderUnderC (note the dollar sign).
A broad question, here are a few general answers.
Enter a remote machine hostname
Set up a GUI for that (WinForms or whatever you like)
get list folder names in C directory from the remote machine
Look into remote directory services, especially Samba / SMB setup and access for Windows. This question will be usefull.
select folder names from the list
With the appriopiate GUI elements (a TreeView maybe), easily possible.
delete the selected folders
Issue a File.Delete() command for the appropiate path, see link above.
show a message about the process (deleted or not)
Wrap above command in a try-catch, then call MessageBox.Show() or whatever GUI elements you want for that.

Unauthorized access trying to save .rtf file [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 7 years ago.
Improve this question
I'm having an issue in saving an rtf file, I'm saving it in a subfolder in "MyDocuments" the strange this is that i also save a .bin file and some other stuff and it doesn't causes any exeption, i'am pc admin so why does it happen?
string docs = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Monitor\\Immobili\\";`
richTextBox1.SaveFile(docs+code);
code is also another string
An UnauthorizedAccessException means one of 3 things:
The caller does not have the required permission.
Path is a directory.
Path specified a read-only file.
Check for last two reasons. I think you are not adding filename after path. Add breakpoints and check if richTextBox1.SaveFile(docs+code); is getting the complete path to the file instead of just folder path.

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