【问题标题】:Accessing variable in another namespace C#访问另一个命名空间 C# 中的变量
【发布时间】:2017-04-30 06:22:20
【问题描述】:

我知道这已经在这里讨论过很多次了,我尝试将我的变量设置为公共等,但我似乎无法在我的其他名称空间中使用它。如果您能给我一些提示,我将不胜感激(我是 C# 的菜鸟) 我尝试从其他名称空间访问的字符串是“stringatlinei”

using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Security;
using System.Security.Cryptography;
using System.IO;
using System.Net;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Net.Mail;
using ConsoleApplication32;





namespace ConsoleApplication32

{
public class Program
{




    static void Main(string[] args)
    {
        DirSearch(@"C:\\Users\\");
        Console.ReadKey();
    }





    static void DirSearch(string dir)
    {
        try
        {

            foreach (string d in Directory.GetDirectories(dir))
            {
                Console.WriteLine(d);

                DirSearch(d);

                // Compose a string that consists of three lines.
                string lines = d;

                // Write the string to a file.
                System.IO.StreamWriter file = new         
               System.IO.StreamWriter("c:\\users\\chris\\desktop\\test.txt", true);
                file.WriteLine(lines);

                file.Close();

                var oldLines = System.IO.File.ReadAllLines("c:\\users\\chris\\desktop\\test.txt");
                var newLines = oldLines.Where(line => !line.Contains("Windows"));
                System.IO.File.WriteAllLines("c:\\users\\chris\\desktop\\test.txt", newLines);

                var oldLines4 = System.IO.File.ReadAllLines("c:\\users\\chris\\desktop\\test.txt");
                var newLines4 = oldLines.Where(line => !line.Contains("$Recycle.Bin"));
                System.IO.File.WriteAllLines("c:\\users\\chris\\desktop\\test.txt", newLines4);

                var oldLines7 = System.IO.File.ReadAllLines("c:\\users\\chris\\desktop\\test.txt");
                var newLines7 = oldLines.Where(line => !line.Contains("Program Files"));
                System.IO.File.WriteAllLines("c:\\users\\chris\\desktop\\test.txt", newLines7);

                var oldLines8 = System.IO.File.ReadAllLines("c:\\users\\chris\\desktop\\test.txt");
                var newLines8 = oldLines.Where(line => !line.Contains("Program Files (x86)"));
                System.IO.File.WriteAllLines("c:\\users\\chris\\desktop\\test.txt", newLines8);

                var oldLines9 = System.IO.File.ReadAllLines("c:\\users\\chris\\desktop\\test.txt");
                var newLines9 = oldLines.Where(line => !line.Contains("AppData"));
                System.IO.File.WriteAllLines("c:\\users\\chris\\desktop\\test.txt", newLines9);













            }

        }
        catch (System.Exception ex)
        {
            Console.WriteLine(ex.Message);


        }



        var lineCount = File.ReadLines(@"c:\\users\\chris\\desktop\\test.txt").Count();
        Console.WriteLine(lineCount);
        Console.ReadLine();
        int element = 0;

        for (int i = 0; i < lineCount + 1; i++)

        {

            element = element + 1;
            String stringAtLinei = File.ReadLines("c:\\users\\chris\\desktop\\test.txt").ElementAtOrDefault(element);
            Console.WriteLine(stringAtLinei);

        }




        System.Diagnostics.Process process = new System.Diagnostics.Process();
        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
        startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        startInfo.FileName = "cmd.exe";
        startInfo.Arguments = "random command example thing";
        process.StartInfo = startInfo;
        process.Start();





        }
    }
}

然后是其他命名空间等。

【问题讨论】:

    标签: c# string visual-studio function namespaces


    【解决方案1】:

    “stringatlinei”不是属性,也不是公共的东西,它是在函数内部的循环中定义的。

    尝试公共静态字符串 stringatlinei="",然后在循环中分配。

    【讨论】:

      【解决方案2】:

      无法在另一个命名空间中访问该变量。您应该阅读基础知识,特别是范围,以了解为什么会这样。这是一篇有用的文章:https://msdn.microsoft.com/en-us/library/aa691132(v=vs.71).aspx

      基本上,您尝试访问的变量是在 for 循环中创建的。循环的每次迭代,它都会消失。

      【讨论】:

        【解决方案3】:

        这是您应该更改的部分:(为了清楚起见,我删除了其余部分)

        namespace ConsoleApplication32
        {
            public class Program
            {
                public String stringAtLinei;
        
                static void DirSearch(string dir)
                {
                     for (int i = 0; i < lineCount + 1; i++)
                     {
                          stringAtLinei = File.ReadLines("c:\\users\\chris\\desktop\\test.txt").ElementAtOrDefault(element);
        
                     }
                }
            }
        }
        

        关键是你可以访问类的字段和属性,但不能访问仅限于它们所在方法的变量。所以我将变量更改为类中的字段。

        【讨论】:

        • 谢谢,当我可以访问我的计算机和 Visual Studio 时,我会尝试一下。我正要放弃
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-08-06
        • 2021-01-01
        • 2017-01-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-23
        相关资源
        最近更新 更多