List All Partitions On Disk [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
I'm making a utility in C# for a filesystem that isn't supported by windows, which means that I can't just access the drive. I need a way to list all partitions on the hard disk and writing/formatting them.

To list disk partitions you can use WMI.
var searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_DiskPartition");
foreach (var queryObj in searcher.Get())
{
Console.WriteLine("-----------------------------------");
Console.WriteLine("Win32_DiskPartition instance");
Console.WriteLine("Name:{0}", (string)queryObj["Name"]);
Console.WriteLine("Index:{0}", (uint)queryObj["Index"]);
Console.WriteLine("DiskIndex:{0}", (uint)queryObj["DiskIndex"]);
Console.WriteLine("BootPartition:{0}", (bool)queryObj["BootPartition"]);
}

You can use the following approach to get the Volume or DriveLetter on which the partition of disk is mounted.
Win32_LogicalDiskToPartition
Win32_DiskDrive
From the Win32_DiskDrive class you can get the DriveNumber by querying property Index or extracting the DriveNumber from Name attribute. Then query Antecedent and Dependent from Win32_LogicalDiskToPartition. In the Antecedent value you will get the Disk Number and the partition it is trying to map the Volume, after that extract the DriveLetter such as "C:", "D:" etc from the Dependent property. So by using this logic you can get the LogicalDrives mounted on particular HardDisk.
I'm using this logic in my component to get the LogicalDrive names ("C:", "D:" etc) for particular hard drive on my system.

Related

Is there any alternative for netstat command in C#? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I am writing a C# program which tells the Active Connections (Protocal, Local Address , Foreign Address , State , Process Id ) . To do this I am running cmd.exe as a process and passing netstat -ano as an argument.
System.Diagnostics.Process.Start("cmd.exe","netstat -ano");
This returns Active connections .
But I don't want to use this netstat command .
Is there any alternative for netstat.exe in C# ?Any library or something else so that I can get the same output ?
You do not need to run nmap or netstat, you can get all the info much easier when you take a look at the System.Net.NetworkInformation namespace. There you can find charming things like GetActiveTcpListeners() or GetActiveTcpConnections() and much more.
public static void ShowActiveTcpConnections()
{
Console.WriteLine("Active TCP Connections");
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
TcpConnectionInformation[] connections = properties.GetActiveTcpConnections();
foreach (TcpConnectionInformation c in connections)
{
Console.WriteLine("{0} <==> {1}",
c.LocalEndPoint.ToString(),
c.RemoteEndPoint.ToString());
}
}
Details are here in the docs:
https://learn.microsoft.com/en-us/dotnet/api/system.net.networkinformation.ipglobalproperties.getactivetcpconnections?view=net-5.0
I was looking for an netstat alternative as you and I found nmap.
To scan I usually run:
nmap -sP 192.168.1.*

Remove execute permission from uploaded folders and files [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
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
I am working on file and folder upload system and i want to add some security to it.
i have followed this Article , The security point number 6 on it says:
6. Keep tight control of permissions
Any uploaded file will be owned by the web server. But it only needs
read/write permission, not execute permissions. After the file is
downloaded, you could apply additional restrictions if this is
appropriate. Sometimes it can be helpful to remove the execute
permission from directories to prevent the server from enumerating
files.
How to apply that using C#
If I'm understanding you correctly, you want to upload a file to a remote server and then change the file to read only. Here is one option. Start by getting a File Object. After that you can set the access control then supply the access you want to provide.
It might be something like this:
using System.IO;
using System.Security.AccessControl;
private void SetFileAccess(string path)
{
var fileSecurity = new FileSecurity();
var readRule = new FileSystemAccessRule("identityOfUser", FileSystemRights.ReadData, AccessControlType.Allow);
var writeRule = new FileSystemAccessRule("identityOfUser", FileSystemRights.WriteData, AccessControlType.Allow);
var noExecRule = new FileSystemAccessRule("identityOfUser", FileSystemRights.ExecuteFile, AccessControlType.Deny);
fileSecurity.AddAccessRule(readRule);
fileSecurity.AddAccessRule(writeRule);
fileSecurity.AddAccessRule(noExecRule);
File.SetAccessControl(path, fileSecurity);
}
MSDN Link to File
MSDN Link to SetAccessControl Method
MSDN Link to File System Rights

Switch active view between two Revit Documents [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
I have a doc that is already opened in revit. A C# addin is activated and a form(modal?) is opened. A button event is then implemented to open a doc to transfer some mass elements from that file and document to the already opened document.
//the rvt file containing the mass elements opening code.
Document doc = app.OpenDocumentFile(UserRVTFilePath);
I then would like to switch the active document back to the first opened document. Is there a Revit API method that exists to achieve this?
Have you tried using the UIApplication.OpenAndActivateDocument(string revitPath) method?
public Result Execute(ExternalCommandData cmdData, ref string message, ElementSet elements)
{
UIDocument uiDoc = cmdData.Application.OpenAndActivateDocument(#"c:\project.rvt");
// do stuff with uiDoc
return Result.Succeeded;
}
Also you can get a reference to current Active UIDocument using: UIApplication.ActiveUIDocument

how to automap a network drive and check if drive letter is in use [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm fairly new to WebDAV , how do I go about coding in vb or C# to automap a network drive and check if letter G:\ is available and if it is , assign that letter, if not go to H , I, J ,K drives ?
thank you in advance
You can start a process to add the network drive. Then you can check to see when and how p finishes.
Dim p As Process
p = System.Diagnostics.Process.Start("net.exe", "use r: \\reo\c-drive")
Here is one way to check the availability of a drive letter:
If System.IO.Directory.Exists("c:\") Then ...
You can also use Windows API calls, which are messier to write but don't require a separate process.
It's usually not necessary to map a network drive, however, because you can generally use the network path instead of the mapped drive letter.

Is it possible to create an application to check every opened file on windows? [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'd like to build an application (if there isn't already one) to log every Windows (7, 64-bit) opened file (including attributes) on C#.
What do you think? Could it be possible? Is there another software already built to do this? Do I Need to use a low-level language (C?) and manage kernel events?
I mean, if I open this c:/programs/file.exe /my-attributes how can I catch /my-attributes
What you are really asking for is the command line that was used to start a process. An entirely different problem from trying to discover what files are opened by a process. And one that you can actually do with C#, using System.Management to run a query on the Win32_Process class. Start a new console mode project and add a reference to System.Management. Make the code look like this:
using System;
using System.Management; // NOTE: add a reference to System.Management!
class Program {
static void Main(string[] args) {
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT * FROM Win32_Process");
foreach (ManagementObject queryObj in searcher.Get()) {
Console.WriteLine(queryObj["CommandLine"]);
}
Console.ReadLine();
}
}
Beware that the output is not very clean and requires parsing. Some processes may appear with their internal Windows path (like \??\etc), some processes don't have a full path name. Review the Win32_Process class documentation to see what other properties are available.

Categories