2016-12-14 40 views
0

我知道這已經在這裏討論了很多次了,我試圖將變量設置爲公共等,但我似乎無法在我的其他名稱空間中使用它。我會很感激,如果你能給我一些提示(我在C#中的小白) 我試圖從我的其他名字空間訪問字符串是「stringatlinei」在另一個命名空間C中訪問變量#

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(); 





     } 
    } 
} 

那麼其他的命名空間等。

回答

0

「stringatlinei」不是一個屬性或公共任何東西,它是在一個函數內部的循環內定義的。

嘗試public static string stringatlinei =「」,然後在您的循環中進行賦值。

0

下面是你應該改變的部分:(我刪除爲清晰起見,其餘部分)

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); 

      } 
     } 
    } 
} 

的一點是,你可以訪問域和類的屬性,而不是僅限於方法的變量,他們'所以我將變量改爲班級中的一個字段。

+0

謝謝我會試試看,當我進入我的電腦和視覺工作室。我正要放棄 –