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.
Related
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!
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.
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 7 years ago.
Improve this question
I have a cs page with a few methods. When I try to load that page it takes a long time. Is there any way to identify how long each method is taking to fetch data? Instead of debugging is there any way of capturing the time spans?
Any tool or any other suggestions?
Yes you can enable Tracing to check which event takes how much time.
set Trace = true in the Page directive.
Also you can write custom trace line
Trace.Write("Method 1 begin");
//Call you method thats taking time.
CalculateSomething();
Trace.Write("Method 1 begin");
And you can use stopwatch to check the exact time.
using System.Diagnostics;
Stopwatch St = new Stopwatch();
St.Start();
//Call your method
Trace.Write("Stopwatch " + St.ElapsedTime.toString());
St.Stop();
The Visual Studio Profiling Tools let developers measure, evaluate, and target performance-related issues in their code. These tools are fully integrated into the IDE to provide a seamless and approachable user experience.
Profiling an application is straightforward. You begin by creating a new performance session. In Visual Studio Team System Development Edition, you can use the Performance Session Wizard to create a new performance session. After a performance session ends, data gathered during profiling is saved in a .vsp file. You can view the .vsp file inside the IDE. There are several report views available to help visualize and detect performance issues from the data gathered.
MSDN REFERENCE
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I look after a website which has news/blog section. As usual, admin can create/edit articles. It's been running fine for years but suddenly last week, I became to get an error saying "Input string was not in a correct format." when creating/editing articles. And I also get error notification email from the system as below;
Message: Error converting data type varchar to datetime.
The entry page has date fields but I use the same format I've been using and nothing has changed since the last time I created/edited articles successfully. The same files and code in the server. It suddenly started not to let me create/edit anything. Even it won't let me save articles without changing anything of the article.
It's running on ASP.Net(C#), MS SQL Server and IIS. As nothing has changed, I have no idea where to start but can this be happened?? And what would be the cause?
I don't know what exactly the culture setting was in the Windows Server but I don't see any incorrect setting. Current correct Region, Date and Time are there. No one has accessed to the server between the dates something went wrong. The SQL server uses the different setting from the server ones but it has been like this.
The website has some sections and the news/blog section gets the error but the other section, which also uses dates, don't get an error. Obviously these different sections uses the different files. So I think the server/SQL settings are OK. But as I mentioned, the files haven't changed at all.
Any other possibilities?? Thanks for your help.
Try to check if anyone has changed the culture settings on the server (on which your MS SQL Server is running). Control Panel, Region and Language.
DateTime parsing can depend on those settings if it uses the current culture on the machine.
Assumption 1:
Your asp.net application may unnecessary convert DateTime.ToString() when constructing the sql insert/update command.
Assumption 2:
It gets the string as it is and uses the session login default language and format (see SET LANGUAGE / SET FORMAT in SQL). Check if the default language/format has changed in your sql.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
In code I often find ~/ or ../ with paths but unfortunately it is not clear to me what these are and what the difference is. Which one to use with multiple level directories ?
I guess ../ means domain of site or application?
Can you please guide what they are, and how they are different?
~/ is often refereed to in helper functions such as <%= ResolveUrl() %> for example. It refers to the root of the website whereas ../ simply refers to the parent directory. Both are relative urls.
Let's take an example. Suppose that your website is hosted in a virtual directory called MyApplication. When you use <%= ResolveUrl("~/foo/bar") %> it would generate /MyApplication/foo/bar as output url and this no matter in which WebForm location.
There're two kinds of paths:
Regular paths
Virtual paths
When you just use / or ../ you're using regular paths relative to the IIS - the web server - Web site URL. That is, /myfile.txt would be wrong if your application is hosted in a virtual directory called mydir. In this case, /myfile.txt will end in an URL like this: http://www.mydomain.com/myfile.txt, while you expected http://www.mydomain.com/mydir/myfile.txt.
For that reason, ASP.NET gives you the chance to provide virtual paths. All of them start with ~ character. The ~ character specifies that the resolved URI is relative to the IIS application. Taking the above example of expecting http://www.mydomain.com/mydir/myfile.txt, the right virtual path would be ~/myfile.txt.
Note that virtual paths aren't allowed in non-server controls. This kind of path is used in a selected number of ASP.NET class methods and server controls.
The tilde (~) refers to the application root directory. In ASP, the tilde is used for HyperLinks or Page.ResolveURL.
Two dots (..) refers to the folder that is one level higher than the current folder.