- OpenFile function (winbase.h)
- Syntax
- Parameters
- Return value
- Remarks
- openfiles
- openfiles /disconnect
- Syntax
- Parameters
- Examples
- openfiles /query
- Syntax
- Parameters
- Examples
- openfiles /local
- Syntax
- Parameters
- Examples
- How to open, view, and edit the contents of a file on a computer
- Microsoft Windows users
- Alternative method
- MS-DOS and Windows command line users
- List files
- Edit command
- Start command
- Type command
- Copy con
- macOS users
- Microsoft Windows Explorer
- Unix and Linux users
- List files
- Opening, creating, editing, and viewing files in Linux
OpenFile function (winbase.h)
Creates, opens, reopens, or deletes a file.
Syntax
Parameters
The name of the file.
The string must consist of characters from the 8-bit Windows character set. The OpenFile function does not support Unicode file names or opening named pipes.
A pointer to the OFSTRUCT structure that receives information about a file when it is first opened.
The structure can be used in subsequent calls to the OpenFile function to see an open file.
The OFSTRUCT structure contains a path string member with a length that is limited to OFS_MAXPATHNAME characters, which is 128 characters. Because of this, you cannot use the OpenFile function to open a file with a path length that exceeds 128 characters. The CreateFile function does not have this path length limitation.
The action to be taken.
This parameter can be one or more of the following values.
Value | Meaning |
---|---|
OF_CANCEL 0x00000800 | Ignored. To produce a dialog box containing a Cancel button, use OF_PROMPT. |
OF_CREATE 0x00001000 | Creates a new file. If the file exists, it is truncated to zero (0) length. |
OF_DELETE 0x00000200 | Deletes a file. |
OF_EXIST 0x00004000 | Opens a file and then closes it. Use this to test for the existence of a file. |
OF_PARSE 0x00000100 | Fills the OFSTRUCT structure, but does not do anything else. |
OF_PROMPT 0x00002000 | Displays a dialog box if a requested file does not exist. A dialog box informs a user that the system cannot find a file, and it contains Retry and Cancel buttons. The Cancel button directs OpenFile to return a file-not-found error message. |
OF_READ 0x00000000 | Opens a file for reading only. |
OF_READWRITE 0x00000002 | Opens a file with read/write permissions. |
OF_REOPEN 0x00008000 | Opens a file by using information in the reopen buffer. |
OF_SHARE_COMPAT 0x00000000 | For MS-DOS–based file systems, opens a file with compatibility mode, allows any process on a specified computer to open the file any number of times. Other efforts to open a file with other sharing modes fail. This flag is mapped to the FILE_SHARE_READ|FILE_SHARE_WRITE flags of the CreateFile function. |
OF_SHARE_DENY_NONE 0x00000040 | Opens a file without denying read or write access to other processes. On MS-DOS-based file systems, if the file has been opened in compatibility mode by any other process, the function fails. This flag is mapped to the FILE_SHARE_READ|FILE_SHARE_WRITE flags of the CreateFile function. |
OF_SHARE_DENY_READ 0x00000030 | Opens a file and denies read access to other processes. On MS-DOS-based file systems, if the file has been opened in compatibility mode, or for read access by any other process, the function fails. This flag is mapped to the FILE_SHARE_WRITE flag of the CreateFile function. |
OF_SHARE_DENY_WRITE 0x00000020 | Opens a file and denies write access to other processes. On MS-DOS-based file systems, if a file has been opened in compatibility mode, or for write access by any other process, the function fails. This flag is mapped to the FILE_SHARE_READ flag of the CreateFile function. |
OF_SHARE_EXCLUSIVE 0x00000010 | Opens a file with exclusive mode, and denies both read/write access to other processes. If a file has been opened in any other mode for read/write access, even by the current process, the function fails. |
OF_VERIFY | Verifies that the date and time of a file are the same as when it was opened previously. This is useful as an extra check for read-only files. |
OF_WRITE 0x00000001 | Opens a file for write access only. |
Return value
If the function succeeds, the return value specifies a file handle to use when performing file I/O. To close the file, call the CloseHandle function using this handle.
If the function fails, the return value is HFILE_ERROR. To get extended error information, call GetLastError.
Remarks
If the lpFileName parameter specifies a file name and extension only, this function searches for a matching file in the following directories and the order shown:
- The directory where an application is loaded.
- The current directory.
- The Windows system directory.
Use the GetSystemDirectory function to get the path of this directory.
The 16-bit Windows system directory.
There is not a function that retrieves the path of this directory, but it is searched.
The Windows directory.
Use the GetWindowsDirectory function to get the path of this directory.
The lpFileName parameter cannot contain wildcard characters.
The OpenFile function does not support the OF_SEARCH flag that the 16-bit Windows OpenFile function supports. The OF_SEARCH flag directs the system to search for a matching file even when a file name includes a full path. Use the SearchPath function to search for a file.
A sharing violation occurs if an attempt is made to open a file or directory for deletion on a remote machine when the value of the uStyle parameter is the OF_DELETE access flag OR’ed with any other access flag, and the remote file or directory has not been opened with FILE_SHARE_DELETE share access. To avoid the sharing violation in this scenario, open the remote file or directory with OF_DELETE access only, or call DeleteFile without first opening the file or directory for deletion.
In WindowsВ 8 and Windows ServerВ 2012, this function is supported by the following technologies.
Technology | Supported |
---|---|
Server Message Block (SMB) 3.0 protocol | Yes |
SMB 3.0 Transparent Failover (TFO) | Yes |
SMB 3.0 with Scale-out File Shares (SO) | Yes |
Cluster Shared Volume File System (CsvFS) | Yes |
Resilient File System (ReFS) | Yes |
В
CsvFs will do redirected IO for compressed files.
openfiles
Enables an administrator to query, display, or disconnect files and directories that have been opened on a system. This command also enables or disables the system Maintain Objects List global flag.
openfiles /disconnect
Enables an administrator to disconnect files and folders that have been opened remotely through a shared folder.
Syntax
Parameters
Parameter | Description |
---|---|
/s | Specifies the remote system to connect to (by name or IP address). Don’t use backslashes. If you don’t use the /s option, the command is run on the local computer by default. This parameter applies to all files and folders that are specified in the command. |
/u [ \] | Runs the command using the permissions of the specified user account. If you don’t use the /u option, system permissions are used by default. |
/p [ |
]
Note: You can use the openfiles /query command to find the file ID.
Examples
To disconnect all open files with the file ID 26843578, type:
To disconnect all open files and directories accessed by the user hiropln, type:
To disconnect all open files and directories with read/write mode, type:
To disconnect the directory with the open file name *C:\testshare*, regardless of who is accessing it, type:
To disconnect all open files on the remote computer srvmain that are being accessed by the user hiropln, regardless of their ID, type:
openfiles /query
Queries and displays all open files.
Syntax
Parameters
Parameter | Description |
---|---|
/s | Specifies the remote system to connect to (by name or IP address). Don’t use backslashes. If you don’t use the /s option, the command is run on the local computer by default. This parameter applies to all files and folders that are specified in the command. |
/u [ \] | Runs the command using the permissions of the specified user account. If you don’t use the /u option, system permissions are used by default. |
/p [ |
]
Displays the output in the specified format. Valid values include:
| |
/nh | Suppresses column headers in the output. Valid only when the /fo parameter is set to TABLE or CSV. |
/v | Specifies that detailed (verbose) information be displayed in the output. |
/? | Displays help at the command prompt. |
Examples
To query and display all open files, type:
To query and display all open files in table format without headers, type:
To query and display all open files in list format with detailed information, type:
To query and display all open files on the remote system srvmain by using the credentials for the user hiropln on the maindom domain, type:
In this example, the password is supplied on the command line. To prevent displaying the password, leave out the /p option. You’ll be prompted for the password, which won’t be echoed to the screen.
openfiles /local
Enables or disables the system Maintain Objects List global flag. If used without parameters, openfiles /local displays the current status of the Maintain Objects List global flag.
Changes made by using the on or off option don’t take effect until you restart the system. Enabling the Maintain Objects List global flag might slow down your system.
Syntax
Parameters
Parameter | Description |
---|---|
[on | off] | Enables or disables the system Maintain Objects List global flag, which tracks local file handles. |
/? | Displays help at the command prompt. |
Examples
To check the current status of the Maintain Objects List global flag, type:
By default, the Maintain Objects List global flag is disabled, and the following message appears, INFO: The system global flag ‘maintain objects list’ is currently disabled.
To enable the Maintain Objects List global flag, type:
The following message appears when the global flag is enabled, SUCCESS: The system global flag ‘maintain objects list’ is enabled. This will take effect after the system is restarted.
To disable the Maintain Objects List global flag, type:
How to open, view, and edit the contents of a file on a computer
A file may be compiled or created only to be viewed by a particular program. The examples below open a plain text file. If the file looks like garbage when opened, the program is not capable of viewing the file. Make sure the program you need to open the file is installed and associated with the file type. If you don’t know what program to use, determine the file extension and review our file extensions page for a listing of associated programs.
Select your operating system in the list below for instructions on viewing the contents of a plain text file.
Microsoft Windows users
Double-click the file that you want to open. The program associated with the file opens that file.
You can open multiple files, such as Word documents, Excel spreadsheets, plain text files, PDF files, and more, at the same time. The associated programs open each file and display them on the computer screen.
If the file is unassociated with a program, you will receive an «Open With» prompt. If you don’t know which program to use, try using WordPad or Notepad.
If the file is associated with a program, but you want to use a different program, hold down the Shift and right-click the file. Select Open With from the drop-down menu that appears, and then select the program you want to use to open the file.
Alternative method
- Open the program you want to use to view the file. For example, if you wanted to view the file in Microsoft Word, you would open it first.
- Once the program is opened, from the file menu, select Open or use the keyboard shortcut Ctrl + O .
- In the Open window, browse to the location of the file, select the file, and then click OK or Open.
Windows users can also use the methods mentioned below to open a file using the Windows command line.
MS-DOS and Windows command line users
List files
If you need to view a list of available files in the current directory, you can use the dir command.
Edit command
Locate the file that you want to view. The example below, we are opening the file autoexec.bat in the current directory.
If the file does not exist, was entered improperly, or is empty, a blank window similar to the example below is shown.
Once you have edited the file or typed the information for the file, click File, and choose Exit. If you do not have a mouse, see the edit command page for keyboard shortcuts and other navigation tips.
After clicking exit, if any changes were made, you’ll be asked if you want to save the file. Click Yes to finalize your changes.
- See our edit command page for further information on this command.
Start command
New, 64-bit versions of Windows no longer support the edit command. You can also use the start command to open a file in a text editor, such as Notepad, using the following command.
In the example above, if the file «hope.txt» did not exist in the current directory, you are prompted to create a new file.
The start command can also be used to open any file on your computer. For example, if you have Microsoft Word installed on the computer and want to open a Word document from the command line, you would enter the following:
- See our start command page for further information.
Type command
If you only want to view the contents of the file, you can also use the type command:
In the example above, the command would display all of the contents of the hope.txt to the file. If the file is a large file, it will keep scrolling unless you press the pause key. For large files, you can also pipe the command to more as shown in the example below.
In the previous example, the type command displays the contents one page at a time. Pressing the Enter will advance the output one line at a time and pressing the spacebar advances one page at a time.
- In MS-DOS, you can also use the type command to display the contents of a text file.
Copy con
If you cannot use any of the methods above, you can also use copy con to create a file.
Once you have entered the above command, the hope.txt file is created.
After you have typed all the lines, you want to be in the file, press and hold Ctrl + Z . After ^Z is shown on the screen, press Enter to save the file.
The copy con method only creates a basic file. It does not allow you to edit a file or go back and fix any errors made while creating a line in the file.
macOS users
On a Mac computer, you can double-click a file to open the file in the software program associated with the type of file you are opening. Once the file is opened, you can view the contents of the file and make changes to it.
You can also open a file by selecting it on the desktop, or in the Finder application. Then press the Command + down arrow key.
Microsoft Windows Explorer
Windows users have an extra utility in the way of Explorer. Any file you need to find can be searched through it. Press the Windows key and «E» at the same to access Windows Explorer.
Unix and Linux users
List files
If you need to view a list of available files in the current directory, you can use the ls command.
Opening, creating, editing, and viewing files in Linux
Because of all the available options for each Linux variants we’ve given this section a page of its own in the following link.