Windows get current window

How do I get the title of the current active window using c#?

I’d like to know how to grab the Window title of the current active window (i.e. the one that has focus) using C#.

7 Answers 7

See example on how you can do this with full source code here:

Edited with @Doug McClean comments for better correctness.

If you were talking about WPF then use:

It supports UTF8 characters.

Loop over Application.Current.Windows[] and find the one with IsActive == true .

Use the Windows API. Call GetForegroundWindow() .

GetForegroundWindow() will give you a handle (named hWnd ) to the active window.

If it happens that you need the Current Active Form from your MDI application: (MDI- Multi Document Interface).

you can use process class it’s very easy. use this namespace

if you want to make a button to get active window.

Not the answer you’re looking for? Browse other questions tagged c# .net windows winforms or ask your own question.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.4.16.39093

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Windows shell command to get the full path to the current directory?

Is there a Windows command line command that I can use to get the full path to the current working directory?

Also, how can I store this path inside a variable used in a batch file?

14 Answers 14

Use cd with no arguments if you’re using the shell directly, or %cd% if you want to use it in a batch file (it behaves like an environment variable).

You can set a batch/environment variable as follows:

sample screenshot from a Windows 7 x64 cmd.exe.

Update: if you do a SET var = %cd% instead of SET var=%cd% , below is what happens. Thanks to jeb.

Quote the Windows help for the set command ( set /? ):

Note the %CD% — expands to the current directory string. part.

Читайте также:  Convert windows from mbr to gpt

This has always worked for me:

For Windows we can use

command is there.

For Windows, cd by itself will show you the current working directory.

For UNIX and workalike systems, pwd will perform the same task. You can also use the $PWD shell variable under some shells. I am not sure if Windows supports getting the current working directory via a shell variable or not.

On Windows:

CHDIR Displays the name of or changes the current directory.

In Linux:

PWD Displays the name of current directory.

Based on the follow up question (store the data in a variable) in the comments to the chdir post I’m betting he wants to store the current path to restore it after changeing directories.

The original user should look at «pushd», which changes directory and pushes the current one onto a stack that can be restored with a «popd». On any modern Windows cmd shell that is the way to go when making batch files.

If you really need to grab the current path then modern cmd shells also have a %CD% variable that you can easily stuff away in another variable for reference.

Windows Batch Script Get Current Drive name

I have a batch file which is on a usb key. I need to know the drive name the batch is in.

Example, if it’s E:\mybatch.bat it should find E:\ same thing for F:\, G:\ etc.. when it’s opened.

How could I do that in batch script. (Windows)

8 Answers 8

%CD% is what you’re looking for. It prints the current working directory of the batch file or command running it. If your batch file is on the root of the drive, it will just print the drive letter, otherwise you’ll have to parse the first 2 characters.

on a flash drive mounted to E:.

Update: As Andriy said in the comments, if you are just looking for the first three characters of the path, then use this instead of %CD%:

This will result in E:\ , for example, anywhere on the drive.

0,2% evaluates just to the [current] drive letter and ‘:’. Accordingly, %CD:

0,3% additionally includes ‘\’. – Andriy M Apr 28 ’11 at 5:37

p0% for the batch file’s drive and location. – Nyerguds Jun 10 ’15 at 7:30

Description: Expands %0 to a drive letter.

If run from inside a .CMD/.BAT file, you can use %

dp0 to get the current/working directory. This one is a little safer as it is aware of UNC paths and such. Reference for the syntax of that variable is available here.

Beware

The existing answers to this question don’t acknowledge that the question is actually asking about two different things:

  1. the drive of the current working directory (“Get Current Drive name”); and
  2. the drive of the batch file (“I need to know the drive name the batch is in”).
Читайте также:  Как вернуть стиль windows

These can be different when the batch file is started from a working directory other than its residing location. Readers should therefore be clear on the difference before determining which solution is relevant to their case.

Drive of the current working directory

This takes the full current working directory and trims it down to the first two characters, which will be a drive letter and a colon, e.g. C: .

Drive of the batch file itself

This trims the full path of the batch file (in %0 ) to just a drive letter and a colon, e.g. C: .

(this is an expanded version of my rejected edit to Kai K’s answer)

GetWindow function (winuser.h)

Retrieves a handle to a window that has the specified relationship (Z-Order or owner) to the specified window.

Syntax

Parameters

A handle to a window. The window handle retrieved is relative to this window, based on the value of the uCmd parameter.

The relationship between the specified window and the window whose handle is to be retrieved. This parameter can be one of the following values.

Value Meaning
GW_CHILD 5 The retrieved handle identifies the child window at the top of the Z order, if the specified window is a parent window; otherwise, the retrieved handle is NULL. The function examines only child windows of the specified window. It does not examine descendant windows.
GW_ENABLEDPOPUP 6 The retrieved handle identifies the enabled popup window owned by the specified window (the search uses the first such window found using GW_HWNDNEXT); otherwise, if there are no enabled popup windows, the retrieved handle is that of the specified window.
GW_HWNDFIRST 0 The retrieved handle identifies the window of the same type that is highest in the Z order.

If the specified window is a topmost window, the handle identifies a topmost window. If the specified window is a top-level window, the handle identifies a top-level window. If the specified window is a child window, the handle identifies a sibling window.

GW_HWNDLAST 1 The retrieved handle identifies the window of the same type that is lowest in the Z order.

If the specified window is a topmost window, the handle identifies a topmost window. If the specified window is a top-level window, the handle identifies a top-level window. If the specified window is a child window, the handle identifies a sibling window.

GW_HWNDNEXT 2 The retrieved handle identifies the window below the specified window in the Z order.

If the specified window is a topmost window, the handle identifies a topmost window. If the specified window is a top-level window, the handle identifies a top-level window. If the specified window is a child window, the handle identifies a sibling window.

GW_HWNDPREV 3 The retrieved handle identifies the window above the specified window in the Z order.

If the specified window is a topmost window, the handle identifies a topmost window. If the specified window is a top-level window, the handle identifies a top-level window. If the specified window is a child window, the handle identifies a sibling window.

GW_OWNER 4 The retrieved handle identifies the specified window’s owner window, if any. For more information, see Owned Windows.

Return value

If the function succeeds, the return value is a window handle. If no window exists with the specified relationship to the specified window, the return value is NULL. To get extended error information, call GetLastError.

Remarks

The EnumChildWindows function is more reliable than calling GetWindow in a loop. An application that calls GetWindow to perform this task risks being caught in an infinite loop or referencing a handle to a window that has been destroyed.

How to get at the current users windows identity?

The site is running on my local IIS 6.1. I Would like to add some features to pull information from our Active Directory (AD). My AD code works on many other projects and on my development server. Here are my attempts at writing out the username:

The results I get are:

  1. NT AUTHORITY\IUSR
  2. administrator
  3. NT AUTHORITY\NETWORK SERVICE

How can I get at the actual windows username like ourdomain/username

5 Answers 5

This snippet shows how LogonUserIdentity is set (using reflector)

As you can see this has been changed for IIS 7. I believe you are using Windows Authentication + Impersonation so I would go with the last one ( WindowsIdentity.GetCurrent() ) which I am sure is the identity request being run with.

There are two different windows user here — first one is your application user and second is user (or windows account) under which your ASP.NET application (application pool from IIS perspective) is running. WindowsIdentity.GetCurrent will typically return this reference.

To getting actual windows user that using the application, you must enforce authentication. To do that, you can enable integrated authentication (windows authentication) in IIS for the said web site. Also modify your ASP.NET configuration to use windows authentication. Now you can use HttpContext.Current.User.Identity to get the actual user.

HttpContext.Current.User.Identity may be of use to you here.

Likewise System.Threading.Thread.CurrentPrincipal could help.

But the case might be that you actually have to set an identity instance as the user logs in (though not necessarily implement IPrincipal and the surrounding mechanisms, rather using the built-in WindowsIdentity implementation).

I’m not 100% percent on this, Windows Authentication might set this automatically for you to simply retrieve.

Also, check out this link from MSDN which describes basic user operations,

Читайте также:  Home media server для windows 10
Оцените статью