如何在不調用cmd.exe的情況下將系統(「」)轉換爲C#? 編輯:我需要拋出類似「目錄」system()to c#without call cmd.exe
回答
正如其他人指出,這是Process.Start。例如:
using System.Diagnostics;
// ...
Process.Start(@"C:\myapp\foo.exe");
如果你調用'Process',那麼工作目錄是什麼?開始(@「C:\ myapp \ foo.exe」);'這樣? – Pacerier 2015-04-02 10:26:02
不知道如果我明白你的問題。您是否在尋找Process.Start?
如果我理解正確你的問題,你要找的Process.Start。
見這個例子中(從文檔):
// Opens urls and .html documents using Internet Explorer.
void OpenWithArguments()
{
// url's are not considered documents. They can only be opened
// by passing them as arguments.
Process.Start("IExplore.exe", "www.northwindtraders.com");
// Start a Web page using a browser associated with .html and .asp files.
Process.Start("IExplore.exe", "C:\\myPath\\myFile.htm");
Process.Start("IExplore.exe", "C:\\myPath\\myFile.asp");
}
編輯
正如你說你需要什麼樣的「目錄」命令,我建議你看一看DirectoryInfo 。你可以用它來創建你自己的目錄列表。例如(也是從文檔):
// Create a DirectoryInfo of the directory of the files to enumerate.
DirectoryInfo DirInfo = new DirectoryInfo(@"\\archives1\library");
DateTime StartOf2009 = new DateTime(2009, 01, 01);
// LINQ query for all files created before 2009.
var files = from f in DirInfo.EnumerateFiles()
where DirInfo.CreationTimeUtc < StartOf2009
select f;
// Show results.
foreach (var f in files)
{
Console.WriteLine("{0}", f.Name);
}
你應該首先在你的原始C應用程序中避免使用system()。 – 2010-05-08 14:23:07
dir只是我想模擬cmd.exe的一個例子 – user302823 2010-05-08 15:05:10
無論您試圖用系統實現什麼,都幾乎可以肯定有一種更直接從C/C#執行的更簡單的方法。 *系統*是一個避免的功能。 – 2010-05-09 16:40:38
你實際上想要的相同呢?你可能不會依賴於你想要做什麼。
例如從命令行調用copy
有一個C#相當於本身,File.Copy
,爲dir
有用於獲取信息的整體Directory
類(這些都是快2出的例子十萬)。取決於你以後的工作,C#最有可能爲你試圖運行的特定的命令提供一個庫/函數,通常也是一個更健壯的方法,而不是全局處理程序。
如果全球「調用此」就是你以後,那麼作爲其他的答案表明,使用System.Diagnostics.Process
類是你最好的選擇。
I need to throw something like "dir"
如果你需要運行DIR,那麼你需要調用CMD.EXE爲dir是內部cmd.exe的
如果你想執行一個命令行(cmd.exe的)命令,如「dir」或「time」或「mkdir」,將該命令作爲參數傳遞給cmd.exe並使用標誌/ C。
例如,
cmd.exe /C dir
或
cmd.exe /C mkdir "New Dir"
- 1. php soap call without xsi:type
- 2. asp.net dropdownlist ajax call without ajaxtoolkit
- 3. COBOL CALL「SYSTEM」返回值
- 4. python call to boto3.client.create_data_source_from_s3
- 5. FPDF -PHP -Call to undefined method
- 6. Java | Json String to Object without Library
- 7. PDF to PDF/A-2b without dUseCIEColor
- 8. Boost :: GIL bits8 * to gray8_ptr_t without reinterpret_cast?
- 9. Linq to Sql Group By without count
- 10. Array to string conversion while ajax call from javascript
- 11. Can not setDefaultButton(btn):no object to call upon
- 12. Restlet with GWT to call REST service with HTTP DIGEST
- 13. Javascript string to object reference(without eval()or indexes)
- 14. curl --output append to a file(without >> redirection)
- 15. CMD.exe C#模擬器?
- 16. C#WPF DragMove without Window_LocationChanged()
- 17. MVC Switch V without C?
- 18. C#Deserialization Json array without []
- 19. convert type「system collection generic.iList <long> to long []」
- 20. jquery post call call not works
- 21. windows phone 8.1 call rest api c#
- 22. cout value without touch variable C++
- 23. cmd.exe powershell HashTable
- 24. 這個javascript錯誤是什麼意思? Permission denied to call method to Location.toString
- 25. 使用CMD.EXE
- 26. LINQ to SQL in Visual Studio,InvalidCastException with no call stack
- 27. C#Fmod System :: CreateChannelGroup返回ERR_INVALID_PARAM
- 28. 如何用C#運行system()?
- 29. cmd.exe/k開關
- 30. CMD.exe vs cscript/wscript
忘掉它,如果你想要做的是執行'dir'命令。 'dir'是'cmd.exe'命令行解釋器的內置命令。因此,您不能
拋出**執行**此命令不啓動'cmd.exe'。如果您正在尋找將'system()'調用轉換爲C#的一般方法,請查看下面的答案。 – stakx 2010-05-08 14:35:15