“不安全代码”示例

本示例演示了如何在 C# 中使用非托管代码(使用指针的代码)。

安全说明

提供此代码示例是为了阐释一个概念,它并不代表最安全的编码实践,因此不应在应用程序或网站中使用此代码示例。对于因将此代码示例用于其他用途而出现的偶然或必然的损害,Microsoft 不承担任何责任。

在 Visual Studio 中生成并运行“不安全代码”示例

  1. (-:In Solution Explorer, right-click the FastCopy project, and click Set as StartUp Project.)=100%(在“解决方案资源管理器”中,右击“FastCopy”项目并单击“设为启动项目”。:-)

  2. (-:On the Debug menu, click Start Without Debugging.)=100%(在“调试”菜单上单击“开始执行(不调试)”。:-)

  3. (-:In Solution Explorer, right-click the ReadFile project, and click Set as StartUp Project.)=100%(在“解决方案资源管理器”中,右击“ReadFile”项目并单击“设为启动项目”。:-)

  4. (-:In Solution Explorer, right-click the ReadFile project, and click Properties.)=100%(在“解决方案资源管理器”中,右击“ReadFile”项目并单击“属性”。:-)

  5. 打开“配置属性”文件夹并单击“调试”。

  6. 在“命令行参数”属性中,输入 ..\..\ReadFile.cs

  7. 单击“确定”。

  8. “调试”菜单上单击“开始执行(不调试)”。

  9. (-:In Solution Explorer, right-click the PrintVersion project, and click Set as StartUp Project.)=100%(在“解决方案资源管理器”中,右击“PrintVersion”项目并单击“设为启动项目”。:-)

  10. (-:On the Debug menu, click Start Without Debugging.)=100%(在“调试”菜单上单击“开始执行(不调试)”。:-)

(-:To build and run the Unsafe Code samples from the Command Line)=100%(从命令行生成并运行“不安全代码”示例:-)

  1. (-:Use the Change Directory command to change to the Unsafe directory.)=100%(使用“更改目录”命令转到“Unsafe”目录。:-)

  2. 键入以下命令:

    cd FastCopy
    csc FastCopy.cs /unsafe
    FastCopy
  3. 键入以下命令:

    cd ..\ReadFile
    csc ReadFile.cs /unsafe
    ReadFile ReadFile.cs
  4. 键入以下命令:

    cd ..\PrintVersion
    csc PrintVersion.cs /unsafe
    PrintVersion